Counting the number of characters and words in given string in Java

String regular expression using String.replaceAll()

We can replace a sub string using replaceAll() of String class To show this take one example: Consider We want to change a string result count value given like 1,420,000 we would like to change it to long value we directly convert it to long it will throw java.lang.NumberFormatException. To convert given string value to … Read more

Exception Handling in Java with Examples

To learn about exception handling first you must know what is an exception What is an Exception in Java An exception is run time error that can occur due to wrong user input or due to logical error in programming. Arithmetic Exception Example Consider a simple example. Take two integer number from command prompt and … Read more

How to get the length of the 2D array in Java

Two dimensional array in java To declare an 2D array in java we use the following syntax Datatype [][] arrayName=new Datatype[rows][cols] If you want to declare any primitive 2D array. int [][] ages=new int[3][4]; double [][] prices=new double[4][4] Array ages is a two-dimensional int array, it can store 3 rows and each row can contain … Read more

Print prime numbers from 1 to n in java

Print prime numbers from 1 to n in java using while loop import java.util.Scanner; public class PrimeNumberChecker { public static void main(String[] args) { System.out.println(“Enter a number n”); Scanner scanner = new Scanner(System.in); int number1 = 1; int number2 = scanner.nextInt(); if (number1 >= number2) { System.out.println(“Number2 must be greater then number1”); System.exit(0); } while … Read more

Exception Handling try catch finally blocks in Java

What is try block A try block is used to surround a set of statements where exceptions may occur. A try block used with catch and finally block Also Read Exception Handling in Java: Hierarchy Example and Types What is a catch block A catch block is created followed by a try block. Whenever an … Read more

Throw and Throws Keywords in Java

In java programming, If the exception occurs within the try block it is thrown. Throw and Throws Keywords in Java is very important to throw exceptions. a throw keyword is used to throw an exception A system-generated exception is automatically thrown by the java run time system but if we want to manually throw an … Read more

Java Scanner Class Getting Input from user and file

Java Scanner class is used to take input from users and file. Scanner class is defined as java.util.Scanner It is defined as a final class and extends the Object class and implements Iterator<String> and Closeable interface. public final class Scanner extends Object implements Iterator, Closeable Scanner class reads data in form of tokens and break … Read more

Methods in Java-how to call a method in java

Introducing Method In java programming language classes usually consist of two things instance variable and methods. What is a method in java                                                                  A method is the block of statements that performs a special task. Using methods we can avoid rewriting the same code over and over in a sane program. The method increases the … Read more

Checking whether a file or directory in Java

Checking whether the File object exists if exists then check whether it is file of directory to check file existance exists() method of File class is used if it returns true means file or directory exists, at this point we are not confirm existing File object is file or directory. To check file object isDirectory() … Read more