Java, try-catch with Scanner

26,208

Solution 1

You dont have to do a try catch. This code will do the trick for you :

public static void main(String[] args) {
    boolean wenttocatch = false;
    Scanner scan = new Scanner(System.in);
    int number_of_rigons = 0;
    do{
        System.out.print("Enter a number : ");
        if(scan.hasNextInt()){
            number_of_rigons = scan.nextInt();
            wenttocatch = true;
        }else{
            scan.nextLine();
            System.out.println("Enter a valid Integer value");
        }
    }while(!wenttocatch);
}

Solution 2

The Scanner does not advance until the item is being read. This is mentioned in Scanner JavaDoc. Hence, you may just read the value off using .next() method or check if hasInt() before reading int value.

boolean wenttocatch;
int number_of_rigons = 0;
Scanner sc = new Scanner(System.in);

do {
    try {
        wenttocatch = false;
        number_of_rigons = sc.nextInt(); // sc is an object of scanner class
    } catch (InputMismatchException e) {
        sc.next();
        wenttocatch = true;
        System.out.println("xx");
    }
} while (wenttocatch == true);
Share:
26,208
pavithra
Author by

pavithra

i am still a student who loves programming and love to learn programming.

Updated on December 14, 2021

Comments

  • pavithra
    pavithra over 2 years

    I am creating a small algorithm and this is a part of it.

    If the user enters non integer values, I want to output a message and let the user enter a number again:

    boolean wenttocatch;
    
    do 
    {
        try 
        {
            wenttocatch = false;
            number_of_rigons = sc.nextInt(); // sc is an object of scanner class 
        } 
        catch (Exception e) 
        {
            wenttocatch=true;
            System.out.println("xx");
        }
    } while (wenttocatch==true);
    

    I am getting a never ending loop and I can't figure out why.

    How can I identify if the user enters some non integer number?
    If the user enters a non integer number, how can I ask the user to enter again?

    Update
    When I am printing the exception I get 'InputMismatchException', what should I do?

  • pavithra
    pavithra over 8 years
    well what i want is i want to get an integer until he enters an integer the while loop should run again and again
  • applecrusher
    applecrusher over 8 years
    Not quite sure what you mean. You want to get all the integers a user inputs until they don't input anymore integers?
  • Vitruvie
    Vitruvie over 6 years
    Sorry, glanced at this in new posts review and mistakenly thought "well wenttocatch is still true so it's still going to loop". However this is identical to James Jithin's answer.
  • Ola Ström
    Ola Ström about 4 years
    Don't forget to clear the input buffer after a valid input. You need to add: scannerInput.nextLine(); after the last line of this code.
  • Ola Ström
    Ola Ström about 4 years
    Don't forget to clear the input buffer after a valid input. You need to add this code line: scannerInput.nextLine(); after the while line in the above code.
  • Admin
    Admin over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.