Q10
Question 10
Write a program that reads two integer numbers for the variables a and b. If any other character except number (0-9) is entered then the error is caught by NumberFormatException object. After that ex.getMessage() prints the information about the error occurring causes.
Code
/**
 *Solution for Ans. 10
 * @author Jatin Kamboj 8647
 */
import java.util.Scanner;
public class java_5 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("\nThis program tells" +
                " if the given INPUT is a number" +
                " or not");
        checkInput(sc, "\nEnter First Number: ");
        checkInput(sc, "\nEnter Second Number: ");
    }
    private static void checkInput(Scanner sc, String s) {
        try {
            System.out.print(s);
            String a = sc.nextLine();
            int parsedValue1 = Integer.parseInt(a);
            System.out.println("\nThe INPUT => " +
                    parsedValue1 +
                    " you gave is a Number");
        } catch (NumberFormatException ex) {
            System.out.println("Your INPUT => " +
                    ex.getMessage() +
                    " is not a Number");
        }
    }
}Output

Try or Test The Corresponding Code Here
The above notebook runs on cloud, make sure to run the first cell of the notebook to set-up JDK and JRE on the cloud machine.
Last updated