Java Articles

Page 9 of 450

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 942 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 462 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

What is the difference between System.out.println() and System.out.print() in Java?

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

The println() terminates the current line by writing the line separator string. The print() method just prints the given content.Examplepublic class Sample {    public static void main(String args[]) {       System.out.println("Hello");       System.out.println("how are you");       System.out.print("Hello");       System.out.print("how are you");    } }OutputHello how are you Hellohow are you

Read More

How to perform sort using Java?

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

You can sort an array using sort() method of the Arrays class.Exampleimport java.util.Arrays; public class MainClass {    public static void main(String args[]) throws Exception {       int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };       Arrays.sort(array);       printArray("Sorted array", array);       int index = Arrays.binarySearch(array, 1);       System.out.println("Didn't find 1 @ " + index);       int newIndex = -index - 1;       array = insertElement(array, 1, newIndex);       printArray("With 1 added", array);    }     ...

Read More

How to open a plain text file in Java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 414 Views

You can access a plain text using the File class.Exampleimport java.io.File; public class ReadFile {    public static void main(String[] args) {       File f = null;       String str = "data.txt";       try {          f = new File(str);          boolean bool = f.canExecute();          String a = f.getAbsolutePath();          System.out.print(a);          System.out.println(" is executable: "+ bool);       } catch (Exception e) {          e.printStackTrace();       }    } }Output C:\Users\data is executable: true

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