4

I am working on writing a file reader, and the idea is to have the user enter a number that represents the line number from the text file. The variable that holds this number is of type int. However, when the user enters a String instead, Java throws the InputMismatchException exception, and what I want is to have a loop in the catch clause, where I will be looping until the user enters a valid value, i.e. an int. The skeleton looks like this:

 public void _____ throws IOException {
    try {
    // Prompting user for line number
    // Getting number from keyboard
    // Do something with number
    } catch (InputMismatchException e) {
       // I want to loop until the user enters a valid input
       // When the above step is achieved, I am invoking another method here
    }  
}

My question is, what are some possible techniques that could do the validation? Thank you.

andzrev
  • 544
  • 1
  • 6
  • 17

3 Answers3

4
while(true){ 
   try { 
        // Prompting user for line number 
        // Getting number from keyboard 
        // Do something with number 
        //break; 
       } catch (InputMismatchException e) { 
            // I want to loop until the user enters a valid input 
            // When the above step is achieved, I am invoking another method here 
       } 
   } 
Aram Arabyan
  • 2,339
  • 1
  • 17
  • 30
  • 3
    You even don't need use this boolean variable. just use `while(true)` and break the cycle when data is properly read. – Yarg May 03 '12 at 04:17
  • agree with Yarg 7 while(true){ try { // Prompting user for line number // Getting number from keyboard // Do something with number break; } catch (InputMismatchException e) { // I want to loop until the user enters a valid input // When the above step is achieved, I am invoking another method here } } – Aram Arabyan May 03 '12 at 04:19
  • When I tried either solution, I have an infinite loop. What I was looking for is a way to validate the number itself. I was thinking of obtaining the number's ASCII value, and if it is out of an appropriate range, then keep looping. – andzrev May 03 '12 at 04:37
  • did you mean this ? http://stackoverflow.com/questions/1102891/how-to-check-a-string-is-a-numeric-type-in-java – Aram Arabyan May 03 '12 at 04:43
3

Avoid using exceptions for flow control. Catch the exception, but only print a message. Also, do need for loops within loops.

It's as simple as this:

public void _____ throws IOException {
    int number = -1;
    while (number == -1) {
        try {
            // Prompt user for line number
            // Getting number from keyboard, which could throw an exception
            number = <get from input>;
        } catch (InputMismatchException e) {
             System.out.println("That is not a number!");
        }  
    }
    // Do something with number
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Thank you. This works, but I had to add an extra String ___ = __.nextLine() after the prompt in the `catch` clause. – andzrev May 03 '12 at 05:23
2

You can avoid the Exception

Scanner sc = new Scanner(System.in);
while(sc.hasNextLine())
    String input = sc.nextLine();
    if (isNumeric(input) {
        // do something
        // with the number
        break; // break the loop
    }
}

The method isNumeric:

public static boolean isNumeric(String str) {
    return str.matches("^[0-9]+$");
}

If you want use a dialog for input number:

String input = JOptionPane.showInputDialog("Input a number:"); // show input dialog
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148