1

Below is my code

public class ExceptionHandling {

    public static void main(String[] args) throws InputMismatchException{
        Scanner sc = new Scanner(System.in);
        int a = 0;
        int b = 0;
        try {
            a = sc.nextInt();
            b = sc.nextInt();

            try {
                int c = a / b;
                System.out.println(b);

            } catch (ArithmeticException e) {
                System.out.println(e);
            }
        } catch (InputMismatchException e) {
            System.out.println(e);
        }

    }

}

My main query from the above question is, when I am passing String as input then I am getting java.util.InputMismatchException only. But when I am passing 2147483648 as an input, it gives java.util.InputMismatchException: For input string: "2147483648" as an output.

So can anyone tell me why I am getting For input string: "2147483648" in that case?

Omkar76
  • 1,317
  • 1
  • 8
  • 22
P Yoga Prudhvi
  • 107
  • 2
  • 3
  • 11

2 Answers2

1

The value 2147483648 is larger than the maximum value which can fit into a primitive Java integer, which is 2147483647. Java integers only fit any value between -2147483648 and 2147483647 [-231 to 231-1, since java int is a 32-bit integer]. To get around this problem, either use an input within range for integers, or maybe use a wider type, such as long:

long a = 0;
long b = 0;

try {
    a = sc.nextLong();
    b = sc.nextLong();
    // ...
}
catch (Exception e) { }
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Yeah I understand what you are saying, it is out of range for int, i get that. But my main issue is, while passing "hello" the output is java.util.InputMismatchException. But while passing (2147483648) long in int type the output is= java.util.InputMismatchException: For input string: “2147483648”. I want to know why is it printing extra content. I want the output to be same for both Hello and 2147483648. – P Yoga Prudhvi Jan 10 '21 at 07:51
  • 2
    Well passing an out of range value to an integer and passing a string are both triggering the same exception. Why do you need anything finer grain than this? – Tim Biegeleisen Jan 10 '21 at 07:54
  • It's a programming question on Hackerrank,the output should be same for both string and 2147483648. The exception is same in both cases, but for string it is=java.util.InputMismatchException & for 2147483648 it is =java.util.InputMismatchException: For input string: “2147483648”..I dont know why it is printing (For input string: “2147483648”). – P Yoga Prudhvi Jan 10 '21 at 09:01
1

My main issue is, while passing "hello" the output is java.util.InputMismatchException. But while passing (2147483648) long in int type the output is= java.util.InputMismatchException: For input string: "2147483648". I want to know why is it printing extra content.

That is a different question to what you were originally asking, but I will answer anyway.

The reason you are getting "extra content" as in:

java.util.InputMismatchException: For input string: "2147483648"

is that you are printing the exception like this:

System.out.println(e);

This calls toString() on the exception object and prints it. The toString() method for a typical exception is roughly equivalent to this:

public String toString() {
    return e.getClass().getName() + ": " + e.getMessage();
}

If you don't want the exception name, just print the exception message:

System.out.println(e.getMessage());

which would output:

For input string: "2147483648"

(IMO, that is not the kind of message you should be showing to users. It doesn't explain anything!)


I want the output to be same for both Hello and 2147483648.

It will be, I think. For "Hello", the output will be:

java.util.InputMismatchException: For input string: "Hello"

Finally, if you actually want an intelligible error message, you will need to make more extensive modifications to the code. Unfortunately, neither nextInt() or Integer.parseInt(...) give exception messages that explain why an input string is not an acceptable int value.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216