Found 7442 Articles for Java

Java Program to fill an array with random numbers

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

3K+ Views

Let us first crate an array −double[] arr = new double[5];Now, create Random class object −Random randNum = new Random();Now, fill the above array with random numbers −for (int i = 0; i < 5; i++) { arr[i] = randNum.nextInt(); }Example Live Demoimport java.util.Arrays; import java.util.Random; public class Demo {    public static void main(String args[]) {       double[] arr = new double[5];       Random randNum = new Random();       for (int i = 0; i < 5; i++) {          arr[i] = randNum.nextInt();       }       System.out.println("Random numbers = "+Arrays.toString(arr));    } }OutputRandom numbers = [-6.59888981E8, 1.141160731E9, -9.931249E8, 1.335266582E9, 8.27918412E8]

Java program to shift array elements to the left

Alshifa Hasnain
Updated on 07-Aug-2025 18:19:53

1K+ Views

What Is Left Shifting in an Array? Shifting the array elements to the left by the given number of positions is also known as left rotation of the array. The element at the beginning gets pushed out of the front during the shift, but instead of being deleted, they are re-attached at the end of the array. Let us consider an example for a better understanding: Original Array: After 1 Left Shift: We can see in the above figure that, after the first shift, the element "1" is pushed out and then reattached at the end of the ... Read More

Java Program to create random BigInteger within a given range

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

373 Views

Create a Random class object −Random random = new Random();Now let us create a random BigInteger within the range set below −BigInteger i = new BigInteger(1024, random);Example Live Demoimport java.math.BigInteger; import java.util.Random; public class Demo {    public static void main(String[] args) {       Random random = new Random();       BigInteger i = new BigInteger(1024, random);       System.out.println("Random BigInteger = "+i);    } }OutputRandom BigInteger = 172988250696765715389833755481951104504569480256142363412177847577736195982554760981595486734850686498607899498518922990162874103419942352546847073039976287403845518172884710426458735414702299598858093908453406020426533304237452347610248118892069951238970445771615614781904759399589093691142208659284351662649

Java Program to create a BigDecimal from a string type value

Alshifa Hasnain
Updated on 27-Dec-2024 19:15:53

515 Views

In this article, we will learn to create a BigDecimal from a string value in Java. The BigDecimal class in Java is highly versatile and often used for calculations requiring high precision, such as financial transactions or scientific computations. we'll explore two approaches to creating a BigDecimal from a string value, complete with examples. When to Use BigDecimal We can use the BigDecimal in the following conditions − When working with financial calculations requiring precision (e.g., currency operations). When handling scientific computations involving large or highly precise numbers. ... Read More

Java Program to adjust Adjust LocalDate to first Day Of next month with TemporalAdjusters class

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

352 Views

Let us first set a date −LocalDate localDate = LocalDate.of(2019, Month.JUNE, 15);Now, adjust LocalDate to first Day of next month −LocalDate day = localDate.with(TemporalAdjusters.firstDayOfNextMonth());Example Live Demoimport java.time.LocalDate; import java.time.Month; import java.time.temporal.TemporalAdjusters; public class Demo {    public static void main(String[] args) {       LocalDate localDate = LocalDate.of(2019, Month.JUNE, 15);       System.out.println("Current Date = "+localDate);       System.out.println("Current Month = "+localDate.getMonth());       LocalDate day = localDate.with(TemporalAdjusters.firstDayOfMonth());       System.out.println("First day of month = "+day);       day = localDate.with(TemporalAdjusters.firstDayOfNextMonth());       System.out.println("First day of next month = "+day);    } }OutputCurrent Date ... Read More

Java program to adjust LocalDate to first day of month with TemporalAdjusters class

karthikeya Boyini
Updated on 29-Sep-2024 02:47:31

2K+ Views

In this article, we will learn how to adjust a LocalDate object to find the first day of the month in Java. The program demonstrates how to take a given date and use the TemporalAdjusters class to easily get the first day of that month. This functionality is useful in various applications, such as scheduling events or generating monthly reportsTemporalAdjusters provides utility methods for common date adjustments. We will learn how to set a specific date and adjust it to find the first day of the month. Problem Statement Write a Java program that adjusts a LocalDate to find and ... Read More

Java program to get first Friday in a month

Samual Sam
Updated on 29-Sep-2024 02:47:00

713 Views

In this article, we'll explore how to get the first Friday of any given month in Java. The program will use Java's LocalDate class and TemporalAdjusters to automatically calculate the first Friday based on the input date. We will walk through how this works step by step and see how the code performs this task. Problem Statement Write a program in Java to get the first Friday in a month. Below is a demonstration of the same − Input Current date = 2019-04-01 Output Current date = 2019-04-01 First Friday date = 2019-04-05 Steps to get the first Friday in a ... Read More

Java Program to get day of week for the last day of each month in 2019

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

351 Views

For last day of each month, at first create a List collection for day of week −ListdayOfWeek = new ArrayList();Now, get day of week for the last day of each month in 2019, set the year using withYear() and use lastDayOfMonth() and getDayOfWeek methods −for (Month m: Month.values()) {    DayOfWeek days = LocalDate.now().withYear(2019).with(m).with(TemporalAdjusters.lastDayOfMonth()).getDayOfWeek(); }Example Live Demoimport java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; import java.time.temporal.TemporalAdjusters; import java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] argv) {       ListdayOfWeek = new ArrayList();       for (Month m: Month.values()) {          DayOfWeek days = ... Read More

Java program to get display name for day of week in different locale

Samual Sam
Updated on 23-Nov-2024 03:47:56

358 Views

In this article, we will learn how to get the display name for a day of the week in different locales using Java. The DayOfWeek class in Java provides methods to work with days of the week, and with the help of getDisplayName(), you can retrieve the name of a day in different formats based on the locale. Java.util.Locale.getDisplayName() Method The java.util.Locale.getDisplayName(Locale inLocale) method returns a name for the locale that is appropriate for display to the user. This will be the values returned by getDisplayLanguage(), getDisplayCountry(), and getDisplayVariant() assembled into a single string. Steps to get the display ... Read More

Java Program to get the reverse of the NavigableSet

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

115 Views

We will create a NavigableSet collection and add some elements −NavigableSet set = new TreeSet(); set.add("ABC"); set.add("DEF"); set.add("GHI"); set.add("JKL"); set.add("MNO"); set.add("PQR"); set.add("STU");Now, use descendingSet() for the reverse of NavigableSet −NavigableSetreverse = set.descendingSet();Example Live Demoimport java.util.NavigableSet; import java.util.TreeSet; public class Demo {    public static void main(String[] args) {       NavigableSet set = new TreeSet();       set.add("ABC");       set.add("DEF");       set.add("GHI");       set.add("JKL");       set.add("MNO");       set.add("PQR");       set.add("STU");       NavigableSetreverse = set.descendingSet();       System.out.println("NavigableSet = " + set);       ... Read More

Advertisements