Found 33676 Articles for Programming

Java program to generate random numbers string

Alshifa Hasnain
Updated on 02-Jan-2025 19:15:26

612 Views

In this article, we will learn to generate random number strings in Java. Random numbers are vital in programming for tasks like testing, cryptography, and simulations. A common need is generating random numeric strings, such as verification codes or randomized data. This article uses two Java methods Math.random() for simplicity and the Random class for flexibility and efficiency. Problem Statement Create a program to generate a random string consisting of numeric characters. The string's length should be customizable, and each character must be selected randomly.  Input num[] = { '0', '1', '2', '3', '4', '5' }; Output 33241 A random ... Read More

How to convert Integer array list to float array in Java?

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

1K+ Views

To convert integer array list to float array, let us first create an integer array list −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(25); arrList.add(50); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);Now, convert integer array list to float array. We have first set the size to the float array. With that, each and every value of the integer array is assigned to the float array −final float[] arr = new float[arrList.size()]; int index = 0; for (final Integer value: arrList) {    arr[index++] = value; }Example Live Demoimport java.util.ArrayList; public class Demo {    public static void main(String[] ... Read More

Java program to generate a random number from an array

karthikeya Boyini
Updated on 04-Nov-2024 18:44:53

5K+ Views

In this article, we will learn how to generate a random number from an array of integers in Java by using Random class. The Random class provides methods to generate random numbers, and we will use the nextInt(int bound) method to get a random index within the bounds of our array length. Problem Statement Given an array of integers, we need to randomly select and display one element from the array using Java. Input arr = { 10, 30, 45, 60, 78, 99, 120, 140, 180, 200}; Output Random number from the array = 30 Steps to generate ... Read More

How to generate a random BigInteger value in Java?

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

3K+ Views

To generate random BigInteger in Java, let us first set a min and max value −BigInteger maxLimit = new BigInteger("5000000000000"); BigInteger minLimit = new BigInteger("25000000000");Now, subtract the min and max −BigInteger bigInteger = maxLimit.subtract(minLimit); Declare a Random object and find the length of the maxLimit: Random randNum = new Random(); int len = maxLimit.bitLength();Now, set a new B integer with the length and the random object created above.Example Live Demoimport java.math.BigInteger; import java.util.Random; public class Demo {    public static void main(String[] args) {       BigInteger maxLimit = new BigInteger("5000000000000");       BigInteger minLimit = new BigInteger("25000000000");   ... Read More

Java Program to generate custom random number -1 or 1

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

250 Views

To generate custom random number 1 or -1, you need to use nextBoolean(). At first take a loop and create a Random object on each iteration −for (int i = 0; i < 5; i++) {    Random rand = new Random(); }Now, use nextBoolean() to generate 1 on TRUE condition, ekse -1 −for (int i = 0; i < 5; i++) {    Random rand = new Random();    if (rand.nextBoolean())       System.out.println(1);    else       System.out.println(-1); }Example Live Demoimport java.util.Random; public class Demo {    public static void main(String[] args) {       for ... Read More

Generate 10 random four-digit numbers in Java

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

5K+ Views

To generated random integer, use the Random class with nextInt. At first, create a Random object −Random rand = new Random();The Random above is a random number generator. Now, pick the random numbers one by one. We want 10 random four-digit numbers, therefore loop it until i = 1 to 10 −for (int i = 1; i

Java Program to append all elements of other Collection to ArrayList

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

193 Views

Let us first create an ArrayList −ArrayListarr = new ArrayList(); arr.add("50"); arr.add("100"); arr.add("150"); arr.add("200"); arr.add("250"); arr.add("300");Now, let’s say we have another Collection as Vector −Vectorvector = new Vector(); vector.add("500"); vector.add("700"); vector.add("800"); vector.add("1000"); Append all the elements from the Vector to ArrayList: arr.addAll(vector);Example Live Demoimport java.util.ArrayList; import java.util.Vector; public class Demo {    public static void main(String[] args) {       ArrayListarr = new ArrayList();       arr.add("50");       arr.add("100");       arr.add("150");       arr.add("200");       arr.add("250");       arr.add("300");       Vectorvector = new Vector();       vector.add("500");   ... Read More

Insert an element to List using ListIterator in Java

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

300 Views

Let us first create an ArrayList −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);Now, create a ListIterator from the above ArrayList and insert more elements −ListIterator < Integer > iterator = arrList.listIterator(); iterator.add(1000); iterator.add(2000); iterator.add(3000);Example Live Demoimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayListarrList = new ArrayList();       arrList.add(100);       arrList.add(200);       arrList.add(300);       arrList.add(400);       arrList.add(500);       arrList.add(600);       arrList.add(700);       arrList.add(800);     ... Read More

How to sort List in descending order using Comparator in Java

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

694 Views

Let us first create an ArrayList −ArrayListarrList = new ArrayList(); arrList.add(10); arrList.add(50); arrList.add(100); arrList.add(150); arrList.add(250);Use Comparators interface to order in reverse order with reverseOrder() −Comparator comparator = Collections.reverseOrder(); Now, sort with Collections: Collections.sort(arrList, comparator);Example Live Demoimport java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Demo {    public static void main(String[] args) {       ArrayListarrList = new ArrayList();       arrList.add(10);       arrList.add(50);       arrList.add(100);       arrList.add(150);       arrList.add(250);       arrList.add(100);       arrList.add(150);       arrList.add(250);       Comparator comparator = Collections.reverseOrder();       ... Read More

Java Program to convert LocalDate to java.util.Date

Shriansh Kumar
Updated on 11-Sep-2024 11:12:58

3K+ Views

Suppose a date is given in LocalDate format, our task is to write a Java program that converts it into java.util.Date. For this problem, we need to add time information to the LocalDate by combining it with a time, such as midnight, or a timezone. Both LocalDate and java.util.Date classes are used to represent a date in Java. But, the LocalDate class, introduced with the release of Java8, represents a date without timezone information, whereas, the Date class is mutable and used for representing date and time with timezone. Let's understand the problem statement with an example. Example Scenario: Input: ... Read More

Advertisements