Found 7442 Articles for Java

How to calculate the possibilities of duplication for random number within a range in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

191 Views

To get the duplicate numbers for random numbers in a range, loop through and create two Random class objects −Use nextInt() to get the next number −intrandVal1 = new Random().nextInt(50); intrandVal2 = new Random().nextInt(50);Now, compare both the above numbers −if (randVal1 == randVal2) {    System.out.println("Duplicate number = "+randVal1); }All the above is to be done in a loop −for (int i = 1; i

Java Program to check if a Float is Infinite or Not a Number(NAN)

Samual Sam
Updated on 30-Jul-2019 22:30:25

2K+ Views

To check if a Float is isInfinite, use the isInfinite() method and to check for NAN, use the isNaN() method.Example Live Demopublic class Demo {    public static void main(String[] args) {       float value1 = (float) 1 / 0;       boolean res1 = Float.isInfinite(value1);       System.out.println("Checking for isInfinite? = "+res1);       float value2 = (float) Math.sqrt(9);       boolean res2 = Float.isNaN(value2);       System.out.println("Checking for isNan? = "+res2);    } }OutputChecking for isInfinite? = true Checking for isNan? = false

Java program to get minutes between two time instants

karthikeya Boyini
Updated on 18-Oct-2024 11:58:13

920 Views

In this article, we will calculate the number of minutes between two-time instants using Java. This will be done by using the Instant and Duration classes from the java.time package. We'll create two instances of time, add specific hours and minutes to one of them, and then compute the difference in minutes between the two.Steps to get minutes between two-time instantsFollowing are the steps to get minutes between two-time instants −First, import the necessary classes: Duration, Instant, and ChronoUnit from the java.time package.Create an instance of the current time using Instant.now().Add 5 hours and 10 minutes to the first time instance to ... Read More

Java program to get Milliseconds between two time instants

Samual Sam
Updated on 12-Aug-2024 23:13:17

3K+ Views

In this article, we will demonstrate how to calculate the difference in milliseconds between two Instant objects using the ChronoUnit class. It shows how to work with time-based classes in the java.time package, specifically focusing on creating and manipulating time instants and calculating durations between them. ChronoUnit: A standard set of date and time units lets you manipulate dates, times, and date-times. These units, such as years, months, and days, are designed to work across various calendar systems, even though the specific rules might differ slightly. You can also extend these units by implementing the TemporalUnit interface. Problem Statement Calculate the ... Read More

Java Program to convert java.util.Date to LocalDate

Samual Sam
Updated on 30-Jul-2019 22:30:25

275 Views

At first set the date with java.util.Date −java.util.Date date = new Date();Now, convert the date to LocalDate −Instant instant = Instant.ofEpochMilli(date.getTime()); System.out.println("LocalDate = "+LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate());Example Live Demoimport java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class Demo {    public static void main(String[] args) {       java.util.Date date = new Date();       System.out.println("Date = "+date);       Instant instant = Instant.ofEpochMilli(date.getTime());       System.out.println("LocalDate = "+LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate());    } }OutputDate = Thu Apr 18 23:51:06 IST 2019 LocalDate = 2019-04-18

Java Program to get minimum value with Comparator

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

380 Views

First, declare an integer array and add some elements −Integer arr[] = { 40, 20, 30, 10, 90, 60, 700 };Now, convert the above array to List −List list = Arrays.asList(arr);Now, use Comparator and the reverseOrder() method. Using max() will eventually give you the maximum value, but with reverseOrder() it reverses the result −Comparator comp = Collections.reverseOrder(); System.out.println("Minimum element = "+Collections.max(list, comp));Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Demo {    @SuppressWarnings("unchecked")    public static void main(String args[]) {       Integer arr[] = { 40, 20, 30, 10, 90, 60, 700 };     ... Read More

Java Program to generate random elements from a given array

Samual Sam
Updated on 30-Jul-2019 22:30:25

508 Views

Let’s say the following is our array −Integer[] arr = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};Now, we can convert it to a list before shuffling it −Listlist = Arrays.asList(arr); Collections.shuffle(list);The above shuffling generates random elements. Display them like this −for (Integer res: list) {    System.out.print(res + " "); }Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String[] args) {       Integer[] arr = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};       System.out.print("Array elements...");       for (Integer res: arr) { ... Read More

Java Program to generate random number array within a range and get min and max value

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

1K+ Views

At first, create a double array −double[] val = new double[10];Now, generate and display random numbers in a loop that loops until the length of the above array. We have used nextInt here for random numbers −for (int i = 0; i < val.length; i++) {    val[i] = new Random().nextInt(100);    System.out.println(val[i]); }Now, get the min and max values. Compare each value of the random array with the MIN and MAX values −double min = Double.MAX_VALUE; double max = Double.MIN_VALUE; for (int i = 0; i < val.length; i++) {    if (val[i] < min)       min ... Read More

Java Program to shift array elements to the right

Samual Sam
Updated on 30-Jul-2019 22:30:25

477 Views

Let us first create an int array −int[] arr = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };Now, shift array elements to the right with arraycopy() and placing the elements correctly so that it gets shifted to the right −System.arraycopy(arr, 0, arr, 1, arr.length - 1);Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] argv) throws Exception {       int[] arr = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };       System.out.println("Initial array..."+Arrays.toString(arr));       System.arraycopy(arr, 0, arr, 1, arr.length - 1);       System.out.println("Array after shifting to the right...");       System.out.println(Arrays.toString(arr));    } }OutputInitial array... [10, 20, 30, 40, 50, 60, 70, 80, 90] Array after shifting to the right... [10, 10, 20, 30, 40, 50, 60, 70, 80]

How to shuffle an array in Java?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

3K+ Views

Declare a string array and add elements in the form of letters −String[] letters = { "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };Convert the above array to list −Listlist = Arrays.asList(letters);Now, create a shuffled array using the Random class object and generate the random letters with nextInt() −int len = list.size(); System.out.println("Shuffled array..."); for (int i = 0; i < letters.length; i++) {    int index = new Random().nextInt(len);    String shuffle = list.get(index);    System.out.println(shuffle); }Example Live Demoimport java.util.Arrays; import java.util.List; import java.util.Random; public class Demo {    public static void main(String[] args) {   ... Read More

Advertisements