Java Articles

Page 9 of 450

How to check if array contains three consecutive dates in java?

Nikitha N
Nikitha N
Updated on 11-Mar-2026 1K+ Views

To check to find whether a given array contains three consecutive dates:Convert the given array into a list of type LocalDate.Using the methods of the LocalDate class compare ith, i+1th and i+1th, i+2th elements of the list if equal the list contain 3 consecutive elements.Exampleimport java.time.LocalDate; import java.time.Month; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; public class ConsicutiveDate {    public static void main(String args[]) {       String[] dates = {"5/12/2017", "6/12/2017", "7/12/2017"};       List localDateList = new ArrayList();       for (int i = 0; i

Read More

How to move an element of an array to a specific position (swap)?

Sharon Christine
Sharon Christine
Updated on 11-Mar-2026 3K+ Views

To move an element from one position to other (swap) you need to –Create a temp variable and assign the value of the original position to it.Now, assign the value in the new position to original position.Finally, assign the value in the temp to the new position.Exampleimport java.util.Arrays; public class ChangingPositions {    public static void main(String args[]) {       int originalPosition = 1;       int newPosition = 1;       int [] myArray = {23, 93, 56, 92, 39};       int temp = myArray[originalPosition];             myArray[originalPosition] = myArray[newPosition];       myArray[newPosition] = temp;       System.out.println(Arrays.toString(myArray));    } }Output[23, 39, 56, 92, 93]

Read More

How to Convert a Java 8 Stream to an Array?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 513 Views

To convert a stream to an Array in Java -Collect the stream to a list using the Collect interface and the Collectors class.Now convert the list to an array using the toArray() method.Exampleimport java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class J8StreamToArray {    public static void main(String args[]) {       String[] myArray = { "JavaFX", "OpenCV", "WebGL", "HBase" };       Stream stream = Stream.of(myArray);       List list = stream.collect(Collectors.toList());       String[] str = list.toArray(new String[0]);       System.out.println(Arrays.toString(str));    } }Output[JavaFX, OpenCV, WebGL, HBase]

Read More

How to convert an Array to a Set in Java?

Sai Subramanyam
Sai Subramanyam
Updated on 11-Mar-2026 4K+ Views

One solution to add an array to set is to use the addAll() method of the Collections class. This method accepts a collection and an element and, it adds the given element to the specified Collection. Example Live Demo import java.util.Collections; import java.util.HashSet; import java.util.Set; public class ArrayToSet { public static void main(String args[]) { Integer[] myArray = {23, 93, 56, 92, 39}; Set set = new HashSet(); Collections.addAll(set, myArray); System.out.println(set); } } Output [23, 39, 56, 92, 93]

Read More

How to convert a Double array to a String array in java?

Sharon Christine
Sharon Christine
Updated on 11-Mar-2026 4K+ Views

You can convert a double array to a string using the toString() method. To convert a double array to a string array, convert each element of it to string and populate the String array with them.Exampleimport java.util.Arrays; public class DoubleArrayToString {    public static void main(String args[]) {       Double[] arr = {12.4, 35.2, 25.6, 98.7, 56.4};       int size = arr.length;       String[] str = new String[size];           for(int i=0; i

Read More

How to find consonants in a given string using Java?

George John
George John
Updated on 11-Mar-2026 4K+ Views

One way to find the vowels in a given String is to compare every character in it using the charAt() method with the vowel letters. Example public class FindingConsonants { public static void main(String args[]) { String str = new String("Hi Welcome to Tutorialspoint"); for(int i=0; i

Read More

How to extract the first n characters from a string using Java?

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 944 Views

To find the consonants in the given String compare every character in it using the charAt() method with the vowel letters and remaining are consonants.Examplepublic class FindingConsonants {    public static void main(String args[]) {       String str = new String("Hi Welcome to Tutorialspoint");       for(int i=0; i

Read More

How to extract the last n characters from a string using Java?

Vikyath Ram
Vikyath Ram
Updated on 11-Mar-2026 12K+ Views

To extract last n characters, simply print (length-n)th character to nth character using the charAt() method.Examplepublic class ExtractingCharactersFromStrings {    public static void main(String args[]) {           String str = "Hi welcome to tutorialspoint";       int n = 5;       int initial = str.length()-5;       for(int i=initial; i

Read More

How to create a string from a Java Array?

Arushi
Arushi
Updated on 11-Mar-2026 464 Views

You can convert an array of Strings to a single array using the collect() method.Exampleimport java.util.Arrays; import java.util.stream.Collectors; public class CreatngStringFromArray {    public static void main(String args[]) {       String [] myArray = {"Welcome", "to", "Tutorialspoint"};       String str = Arrays.stream(myArray).collect(Collectors.joining(" "));       System.out.println(str);    } }OutputWelcome to Tutorialspoint

Read More

How to capture divide by zero exception in Java?

Swarali Sree
Swarali Sree
Updated on 11-Mar-2026 2K+ Views

When you divide a number by zero an Arithmetic Exception number is thrown.Examplepublic class DividedByZero {    public static void main(String args[]) {       int a, b;       try {          a = 0;          b = 54/a;          System.out.println("hello");       } catch (ArithmeticException e) {          System.out.println("you cannot divide a number with zero");       }    } }Outputyou cannot divide a number with zero

Read More
Showing 81–90 of 4,495 articles
« Prev 1 7 8 9 10 11 450 Next »
Advertisements