1

i am begginer with java and i made a code which the user puts a number but if he writes a string instead of integer it's a error, how can i control the error to show a message to the user? Here's the error:

Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at Main.main(Main.java:21)

Process finished with exit code 1

And here's my code:

import java.util.Scanner;
import java.util.ArrayList;

public class Main extends MyZoo {

    public static void main(String[] args) {

        System.out.println("1.View all available zoo animals");

        Scanner input = new Scanner(System.in);
        System.out.print("Write Here: ");
        int UserInput = input.nextInt();

        while (true) {

            try
            {
                return input.nextInt();
            }
            catch (InputMismatchException e)
            {
                input.next();
                System.out.print("That’s not an integer. Try again: ");
            }
       }
    }
}

3 Answers3

0

Your try catch looks good, but you should place the first int UserInput = input.nextLine() in the try of course.

Probably you want something like this:

System.out.println("1.View all available zoo animals");

Scanner input = new Scanner(System.in);
while (true) {

    try
    {
        System.out.print("Write Here: ");
        int UserInput = input.nextInt();
        System.out.println("Your input: " + UserInput);
    }
    catch (InputMismatchException e)
    {
        input.next();
        System.out.print("That’s not an integer. Try again: ");
    }
}
Stuck
  • 11,225
  • 11
  • 59
  • 104
0

Your code has some flaws:

  • a main method doesn't (shouldn't) have a return statement
  • you need to import java.util.InputMismatchException
  • you don't assign the user input to your variable UserInput

Here is your code with the above issues removed. As is the code does nothing except getting input and printing it out. Try to add some condition to terminate your programm or add some logic to do something with the input.

import java.util.InputMismatchException;
import java.util.Scanner;

public class NewClass1 {
    public static void main(String[] args) {
        System.out.println("1.View all available zoo animals");

        Scanner input = new Scanner(System.in);

        int UserInput;

        while (true) {
            try
            {
                System.out.println("Write Here: ");
                UserInput = input.nextInt();
                System.out.println("Inputed value: " + UserInput);
            }
            catch (InputMismatchException e)
            {
                input.next();
                System.out.println("That’s not an integer. Try again: ");
            }
        }
    }
}
Eritrean
  • 15,851
  • 3
  • 22
  • 28
0

I think the problem is in the catch part. It should be NumberFormatException instead of InputMismatchException.

The following code block works for you.

public static void main(String[] args) {
    System.out.println("1.View all available zoo animals");
    System.out.print("Write Here: ");

    Scanner scanner = new Scanner(System.in);
    String input = scanner.nextLine();

    while (true) {
        try {
            int userInput = Integer.parseInt(input);
        } catch (NumberFormatException e) {
            System.out.println("Wrong input.");
            System.out.print("Write Here: ");
            input = scanner.nextLine();
        }
    }
}
Tianyu
  • 31
  • 3