Man
Man

Reputation: 43

How do you handle invalid input if you need the user to enter an integer?

I'm trying to write a program that takes user integer inputs and does something with them, and continues to do so until the user enters a non-integer input, at which point the user will no longer be asked for input. This is what I've tried:

import java.io.IOException;

public class Question2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        boolean active = true;
        String num_string_positive = "";
        String num_string_negative = "";
        int my_int;
        while (active) {
            try {
                my_int = in.nextInt();
            } catch(IOException e) {
                active = false;
            }
            in.close(); 
        }
    }

}

This doesn't seem to work; there seems to be something wrong with the catch block. When I hover over IOException, Eclipse says "Unreachable catch block for IOException. This exception is never thrown from the try statement body". Shouldn't a non-integer input throw an I/O exception when I call the nextInt() method?

When I replace catch(IOException e) with catch(Exception e), the code does run, but it always terminates after one input, even if the input is an integer.

What am I doing wrong?

Upvotes: 4

Views: 1654

Answers (7)

Puja Karki
Puja Karki

Reputation: 21

 public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNextInt()) {
      System.out.println("Input is an integer.");
      sc.nextLine(); //Store this input in an array or variable as per your need.
    }
    int number = sc.nextInt();
  }

This code will check if the input is an Integer, if it is then it will continue to take input and as soon as user enters some other value it will stop taking the input.

Upvotes: 2

markspace
markspace

Reputation: 11030

Well you're doing a few things wrong. Besides the obvious (you're catching the wrong exception as you figured out), you're not looping properly, and also you can't use nextInt().

Look at your requirements. You said:

a program that takes user integer inputs and does something with them, and continues to do so until the user enters a non-integer input, at which point the user will no longer be asked for input.

If you use nextInt() how will you find any non-integer input? Clearly you need to do something else here.

Try to break down the program into smaller chunks. You could introduce methods to make your job easier and more clear.

public class Question2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        boolean active = true;
        String num_string_positive = "";
        String num_string_negative = "";
        int my_int;
        while (active) {

          // Get input
          String input = getInput();

          // Is input integer?
          active = isInteger( input );

          // If integer do something
          if( active ) {
             // something
          }

        }

    }

}

If you start by sort of sketching your code out like this, making a sort of outline, then you'll run into fewer problems. Breaking down your code into smaller chunks makes it easier to read, write and understand and debug. Basically wins all the way around.

Upvotes: 1

Amar Kumar
Amar Kumar

Reputation: 2646

Give this a try:

 static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("Enter a number -> ");
            String input = sc.next();
            int intInputValue = 0;
            try {
                intInputValue = Integer.parseInt(input);
            } catch (NumberFormatException ne) {
                System.out.println("This is not a integer");
                break;
            }
        }
    }

The program will display " this is not an integer" if user enters an input other than integer.

Upvotes: 0

VHS
VHS

Reputation: 10194

Eclipse rightly recommends that your catch block trying to catch an IOException is unreachable. This is because none of your code in the corresponding try block throws that particular checked exception. All you have in the try block is a method call Scanner#nextInt. As per the java docs of this method, this method doesn't throw an IOException. Instead, it throws the following three exceptions:

  • InputMismatchException - if the next token does not match the Integer regular expression, or is out of range
  • NoSuchElementException - if input is exhausted
  • IllegalStateException - if this scanner is closed

So in your case, you should update your catch block to catch InputMismatchException instead.

An alternate solution to meet your goal is to use hasNextInt method instead. It will be much cleaner.

On a side note, you shouldn't close the scanner resource until you are done reading.

while (in.hasNextInt()) {   
    my_int = in.nextInt(); 
} 

// close the scanner 
in.close(); 

Upvotes: 1

Praveen
Praveen

Reputation: 1881

while (active) {
    try {
        my_int = in.nextInt();
        active = false;
    } catch(InputMismatchException e) {
        System.out.println("Please enter a valid input!!");
    }
    in.nextLine();//clear the buffer
}
in.close();
  1. You should catch the correct exception to process
  2. You should terminate the while loop with false, only for valid input. What you are trying to do is, you are eliminating when there is a wrong input.
  3. Close the scanner post completion of input retrieval

Upvotes: 1

JArgente
JArgente

Reputation: 2297

First of all. This code will always read only one input because of the in.close method will always execute inside the while loop. In order to close the input only when no integer is read, you have to put it after the while loop and this will execute only when the active flag is false. On the other hand, you cannot catch IOException because nextInt method does not throw it. To catch a more especific exception than Exception, you have to use InputMismatchException

Upvotes: 0

Ismail
Ismail

Reputation: 3042

First, if the input is not an Integer the exception that will be thrown is InputMismatchException, and you shouldn't close the scanner until you continue with it, try this instead:

import java.util.InputMismatchException;

public class Question2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        boolean active = true;
        String num_string_positive = "";
        String num_string_negative = "";
        int my_int;
        while (active) {
            try {
                my_int = in.nextInt();
            } catch(InputMismatchException e) {
                active = false;
                in.close();
            } 
        }
    }

}

Upvotes: 2

Related Questions