Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by karthikeya Boyini
Page 79 of 143
Creating String Object from certain part of a character Array in Java
Here is our character array.char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'};Create string object from some part of a string using the following String constructor. Through this we are fetching substring “IN” from the character array.String str = new String(ch, 4, 2);Examplepublic class Demo { public static void main(String[] args) { char[] ch = { 'T', 'E', 'S', 'T', 'I', 'N', 'G'}; String str = new String(ch, 4, 2); System.out.println(str); } }OutputIN
Read MoreConvert day of year to day of month in Java
Firstly, set the day of year using DAY_OF_YEAR constant.Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, 2018); cal.set(Calendar.DAY_OF_YEAR, 320);Now, get the day of month −int res = cal.get(Calendar.DAY_OF_MONTH);The following is an example −Exampleimport java.util.Calendar; public class Demo { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, 2018); cal.set(Calendar.DAY_OF_YEAR, 320); System.out.println("Date = " + cal.getTime()); int res = cal.get(Calendar.DAY_OF_MONTH); System.out.println("Day of month = " + res); } }OutputDate = Fri Nov 16 07:54:55 UTC 2018 Day of month = 16
Read MoreJava Program to construct one String from another
To construct on string from another, firstly take a charcter array for the first string.char ch[] = { 'A', 'M', 'I', 'T' }; String str1 = new String(ch);The above forms first string. Now, let us created another string from the first string.String str2 = new String(str1);In this way, we can easily construct one string from another.Examplepublic class Demo { public static void main(String[] args) { char ch[] = { 'A', 'M', 'I', 'T' }; String str1 = new String(ch); String str2 = new String(str1); String str3 = ...
Read MoreCreate BigInteger via long type variable in Java
BigInteger class is used for big integer calculations which are outside the limit of the primitive data types. It provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.Firstly, set a long value −Long l = 198L;Now, create a new object for BigInteger and pass the above value −BigInteger bInteger = new BigInteger(l);The following is an example −Exampleimport java.math.BigInteger; public class Demo { public static void main(String[] argv) throws Exception { Long l = 198L; BigInteger bInteger = BigInteger.valueOf(l); System.out.println(bInteger); } }Output198
Read MoreAlign Dropdown to the right with Bootstrap
To align dropdown to the right, add the class .pull-right to .dropdown-menu. You can try to run the following code to align dropdown to the rightExample Bootstrap Example Cars BMW Audi Hyundai Mitsubishi Nissan
Read MoreComparison of double and float primitive types in Java
If we compare a float and a double value with .5 or .0 or .1235 (ending with 5 or 0), then == operator returns true, otherwise it will return false. See the below example.Examplepublic class Tester { public static void main(String[] args) { double d1 = 2.5; float f1 = 2.5f; System.out.println(d1 == f1); double d2 = 2.4; float f2 = 2.4f; double margin = 0.0000001; System.out.println(compareNumbers(d2, f2, margin)); } private static boolean compareNumbers(double d, float f, double margin) { if(Math.abs(d - f) < margin) { return true; } return false; } }Outputtrue true
Read MorePython program to sort out words of the sentence in ascending order
In order to sort the words of a sentence in ascending order, we first need to split the sentence into words using space as the splitting point. For simplicity, we'll only be splitting on space and let the punctuation be there. We can use replace or regex to remove that as well.Once we split the sentence, we can sort the words lexicographically(like in a language dictionary) using either sort or sorted methods depending on whether we want to sort the array in place or sort it, then return a new array.In place sorting: when we want to sort the array/list ...
Read MoreAssigning long values carefully in java to avoid overflow
In case of having the operation of integer values in Java, we need to be aware of int underflow and overflow conditions. Considering the fact that in Java, The int data type is a 32-bit signed two's complement integer having a minimum value of -2, 147, 483, 648 and a maximum value of 2, 147, 483, 647. If a value goes beyond the max value possible, the value goes back to minimum value and continue from that minimum. In a similar way, it happens for a value less than the min value. Consider the following example.Examplepublic class Tester { ...
Read MoreAdd buttons in Bootstrap Navbar
Add buttons using class .navbar-btn to elements. You can try to run the following code to add a button to the navbarExample Bootstrap Example Example Submit Button Navbar Button
Read MoreShow Bootstrap class
To show a Bootstrap class, use the .show class. You can try to run the following code to make a class visibleExample Bootstrap Example This class is visible. This is an example for hide class. This will be hidden.
Read More