Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Java Articles
Page 9 of 450
How to convert an Array to a Set in Java?
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 MoreHow to convert a Double array to a String array in java?
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 MoreHow to find consonants in a given string using Java?
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 MoreHow to extract the first n characters from a string using Java?
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 MoreHow to extract the last n characters from a string using Java?
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 MoreHow to create a string from a Java Array?
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 MoreHow to capture divide by zero exception in Java?
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 MoreWhat is the difference between System.out.println() and System.out.print() in Java?
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 MoreHow to perform sort using Java?
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 MoreHow to open a plain text file in Java?
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