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 68 of 143
Parse and format to arbitrary radix <= Character.MAX_RADIX in Java
Let us set a radix here as.// radix int r = 32;Include the radix here as a BigInteger constructor.BigInteger one = new BigInteger("vv", r);Now get its string representation −String strResult = one.toString(radix);The following is an example −Exampleimport java.math.*; public class Demo { public static void main(String[] args) { // radix int r = 32; BigInteger one = new BigInteger("vv", r); String strResult = one.toString(r); System.out.println(strResult); } }Outputvv
Read MoreMath Operations on BigInteger in Java
Let us apply the following operations on BigInteger using the in-built methods in Java.Addition: add() method Subtraction: subtract() method Multiplication: multiply() method Division: divide() methodLet us create three BigInteger objects.BigInteger one = new BigInteger("98765432123456789"); BigInteger two = new BigInteger("94353687526754387"); BigInteger three = new BigInteger("96489687526737667");Apply mathematical operations on them.one = one.add(two); System.out.println("Addition Operation = " + one); one = one.multiply(two); System.out.println("Multiplication Operation = " + one);The following is an example −Exampleimport java.math.BigInteger; public class Main { public static void main(String[] args) { BigInteger one = new BigInteger("98765432123456789"); ...
Read MoreJava Program to return a Date set to the last possible millisecond of the day before midnight
Let us first set the calendar object.Calendar calendar = Calendar.getInstance();Use the getMaximum() method in Java to returns the maximum value for the given calendar field. We will use it to set the minute, hours second and milliseconds.For hour and minute.calendar.set(Calendar.HOUR_OF_DAY, calendar.getMaximum(Calendar.HOUR_OF_DAY)); calendar.set(Calendar.MINUTE, calendar.getMaximum(Calendar.MINUTE));For second and milliseconds.// second calendar.set(Calendar.SECOND, calendar.getMaximum(Calendar.SECOND)); // millisecond calendar.set(Calendar.MILLISECOND, calendar.getMaximum(Calendar.MILLISECOND));The following is an example that returns a Date set to the last possible millisecond of the day before midnight.Exampleimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo { public static void main(String[] argv) throws Exception { Calendar calendar = Calendar.getInstance(); // hour ...
Read MoreBigInteger.isProbablePrime() method in Java
TheBigInteger.isProbablePrime(int certainty) returns true if this BigInteger is probably prime, false if it's definitely composite. If certainty is ≤ 0, true is returned.Here, the “certainty” parameter is a measure of the uncertainty that the caller is willing to tolerate: if the call returns true the probability that this BigInteger is prime exceeds (1 - 1/2certainty). The execution time of this method is proportional to the value of this parameter.The following is an example −Exampleimport java.math.BigInteger; public class Demo { public static void main(String[] argv) throws Exception { // create 3 BigInteger ...
Read MoreLine Separator in Java
Strings have no newlines. We can form them into two lines by concatenating a newline string. Use System lineSeparator to get a platform-dependent newline string.The following is an example.Examplepublic class Demo { public static void main(String[] args) { String str = "one" + System.lineSeparator() + "two"; System.out.println(str); } }Outputone twoLet us see another example. On Linux based system, the program will work correctly.Examplepublic class Demo { public static void main(String[] args) { String str = System.lineSeparator(); System.out.println((int) str.charAt(0)); } }Output10
Read MoreDisplay the minimum and maximum value of primitive data types in Java
Every data type in Java has a minimum as well as maximum range, for example, for Float.Min = 1.4E-45 Max = 3.4028235E38Let’s say for Float, if the value extends the maximum range displayed above, it leads to Overflow.However, if the value is less than the minimum range displayed above, it leads to Underflow.The following is the Java Program to display the minimum and maximum value of primitive data types.Examplepublic class Demo { public static void main(String[] args) { System.out.println("Integer Datatype values..."); System.out.println("Min = " + Integer.MIN_VALUE); System.out.println("Max = " + ...
Read MoreRemove newline, space and tab characters from a string in Java
To remove newline, space and tab characters from a string, replace them with empty as shown below.replaceAll("[\t ]", "");Above, the new line, tab, and space will get replaced with empty, since we have used replaceAll()The following is the complete example.Examplepublic class Demo { public static void main(String[] args) { String originalStr = "Demo\tText"; System.out.println("Original String with tabs, spaces and newline: "+originalStr); originalStr = originalStr.replaceAll("[\t ]", ""); System.out.println("String after removing tabs, spaces and new line: "+originalStr); } }OutputOriginal String with tabs, spaces and newline: Demo Text String ...
Read MoreDisplay AM/PM time marker with SimpleDateFormat("a") in Java
You can display AM/PM time marker easily in Java using SimpleDateFormat(“a”).Firstly, to work with SimpleDateFormat class in Java, import the following package.import java.text.SimpleDateFormat;Now, set the format with SimpleDateFormat(“a”) to display AM/PM marker −Format f = new SimpleDateFormat(”a”);Now, get the marker in a string −String strMarker = f.format(new Date());The following is an example −Exampleimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo { public static void main(String[] args) throws Exception { // displaying current date and time Calendar cal = Calendar.getInstance(); ...
Read MoreBoolean Type in Java
To display Boolean type, firstly take two variables and declare them as boolean.boolean val1, val2;Then one by one assign values to both of them, one of them is shown below −val1 = true;Now, use if statement to check and display the Boolean true value.if(val1) System.out.println("This is true and will get displayed!");Let us now see the complete example to work with Boolean Type in Java.Examplepublic class Demo { public static void main(String[] args) { boolean val1, val2; System.out.println("Boolean Type in Java"); val1 = true; if(val1) ...
Read MoreJoin Strings in Java
To join strings in Java, use the String.join() method. The delimiter set as the first parameter in the method is copied for each element.Let’s say we want to join the strings “Demo” and “Text”. With that, we want to set a delimeter $. For that, use the join() method as shown below −String.join("$","Demo","Text");The following is an example.Examplepublic class Demo { public static void main(String[] args) { String str = String.join("$","Demo","Text"); System.out.println("Joined strings: "+str); } }OutputJoined strings: Demo$Text
Read More