0

So I am trying to write a try catch to see if the user inputs a value that isn't between 0-4 or isn't an integer. It works well for values outside of 0-4 however, when it catches a non int, say I enter "W" my console is just spammed:

"Please enter the rank, must be 0 - 4

This is not an integer"

over and over again until I force stop the program. I've tried including a throw exception and that just gives me an exception in thread "main" error. How do I get the code to return to try if it catches an error / why is my code spamming console?

Here is my code:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;



public class GroupProjPersonal {

public static void main (String[] args) throws IOException {
    boolean rational = false;
    Scanner s = new Scanner(System.in);
    while (rational == false) {
        try{

            System.out.println("Please enter the rank, must be 0 - 4");
            int rank = s.nextInt();
            if (rank < 0) {
                System.out.println("Rank must be between 0 and 4.");
                rational = false;
            }else {
                if (rank > 4) {
                    System.out.println("Rank must be between 0 and 4.");
                    rational = false;
                }
                else {
                    rational = true;
                }


            }
        }


        catch (Exception e) {
            System.out.println("This is not an integer");
            rational = false;




        }
    }
}
}  

Not sure why this was marked as a duplicate. The question you linked me to has nothing to do with my question.

Blake
  • 91
  • 6
  • Where is the duplicate question? I've searched for near an hour and can't find it. – Blake Apr 17 '16 at 19:50
  • This is not even remotely close to what you marked it as a duplicate of. I'm asking why my console is being spammed. – Blake Apr 17 '16 at 19:51
  • Did you read the linked question? It contains the answer to your question. Especially this one http://stackoverflow.com/a/13102066/1743880. Read it very carefully. – Tunaki Apr 17 '16 at 20:05
  • 2
    The link contains the solution, which is to skip the word which cannot be parsed. The reason you are getting spammed is you are catching the exception inside the loop and repeating the operation which threw the exception endlessly. – Peter Lawrey Apr 17 '16 at 20:06
  • 1
    Hint as to how to debug this yourself: don't just discard the exception in the `catch` block. For example, use `e.printStackTrace();` to get Java to tell you the exception. – Andy Turner Apr 17 '16 at 20:07
  • Also: catching `Exception` is a bad idea, because it conflates three declared exception types thrown by [`Scanner.nextInt()`](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt()), which each should be handled in different ways. – Andy Turner Apr 17 '16 at 20:30
  • The much better dup is [How to handle infinite loop caused by invalid input using Scanner](http://stackoverflow.com/q/3572160). – Tom Apr 17 '16 at 20:46

0 Answers0