Java Articles

Page 13 of 450

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 966 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 482 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 170 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 433 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

How to find the vowels in a given string using Java?

Arushi
Arushi
Updated on 11-Mar-2026 39K+ Views

You can read a character in a String using the charAt() method. To find the vowels in a given String you need to compare every character in it with the vowel letters. Example public class FindingVowels {    public static void main(String args[]) {       String str = new String("Hi Welcome to Tutorialspoint");       for(int i=0; i

Read More

Is Java matcher thread safe in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data. Java provides the java.util.regex package for pattern matching with regular expressions.Matcher classA Matcher object is the engine that interprets the pattern and performs match operations against an input string. Like the Pattern class, Matcher defines no public constructors. You obtain a Matcher object by invoking the matcher() method on a Pattern object.The Instances of this class are not safe ...

Read More
Showing 121–130 of 4,498 articles
« Prev 1 11 12 13 14 15 450 Next »
Advertisements