How to overcome Exception in thread "main" java.util.InputMismatchException

545 Views Asked by At

Program:


import java.util.Scanner;

public class JavaExercise1 {
    static int sum, num1, num2;
    static Scanner scan = new Scanner(System.in);
    public static void add(){
        System.out.print("Enter first number to be added: ");
        num1 = scan.nextInt();
        System.out.print("Enter second number to be added: ");
        num2 = scan.nextInt();
        sum=num1+num2;
        System.out.println(num1+" + "+num2+" = "+sum);
    }
    static void sub(){
        System.out.print("Enter first number to be substracted: ");
        num1 = scan.nextInt();
        System.out.print("Enter second number to be substracted: ");
        num2 = scan.nextInt();
        sum=num1-num2;
        System.out.println(num1+" - "+num2+" = "+sum);
    }
    static void mul(){

            System.out.print("Enter first number to be multiplied: ");
            num1 = scan.nextInt();
            System.out.print("Enter second number to be multiplied: ");
            num2 = scan.nextInt();
            sum=num1*num2;
            System.out.println(num1+" x "+num2+" = "+sum);
    }
    static void div(){
        try{
            System.out.print("Enter first number to be divided: ");
            num1 = scan.nextInt();
            System.out.print("Entr second number to be divided: ");
            num2 = scan.nextInt();
            sum=num1/num2;
            System.out.println(num1+" / "+num2+" = "+sum);
        }catch(Exception e){
            System.out.println("\nException occured...");
            System.out.println(e);
        }
    }

    static void printInstructions(){
        System.out.println("1. Addition\n2. Substraction\n3. Multiplication\n4. Division\n5. Exit");
    }
    public static void main(String[] args) {
        int ch;
        do{
            System.out.println("\nEnter your choice: (Choose 0 to print instructions)");
            ch = scan.nextInt();
            switch(ch){
                case 0: printInstructions();
                        break;
                case 1: add();
                        break;
                case 2: sub();
                        break;
                case 3: mul();
                    break;
                case 4: div();
                    break;
                case 5: break;
                default:
                    System.out.println("Invalid choice....");
            }
        }while(ch!=5);

    }
}

When I enter a number of greater digits is throws a thread exception (Exception in thread "main" java.util.InputMismatchException)

Output:
Enter your choice: (Choose 0 to print instructions) 4 Enter first number to be divided: 9999999999

Exception occured... java.util.InputMismatchException: For input string: "9999999999"

Enter your choice: (Choose 0 to print instructions) Exception in thread "main" java.util.InputMismatchException: For input string: "9999999999" at java.base/java.util.Scanner.nextInt(Scanner.java:2264) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at JavaExercise1.main(JavaExercise1.java:52)

Process finished with exit code 1

1

There are 1 best solutions below

0
On

Your input 9999999999 is bigger than the largest int in Java: 2147483647.

Change your scan.nextInt to scan.nextLong and also the corresponding integers to long if you want to handle such large numbers.

Alternatively or additionally, use a try-catch block to catch the exception and take appropriate action (like error message to user to try again). This can be a part of normal user input checking. Be very suspicious of user inputs because somebody might type "Prithviraj is the greatest!" instead of a number, for example.

And last but not least, welcome to the Stacks.