0

I simply would like to break out of a while true loop if the user enters in "STOP" into the scanner. Currently my scanner only takes in integers and I believe this is where the problem lies. If I try to type in "STOP" I get many errors saying "exception in thread main". Here is a snippet of my code:

public class algorithm {
private Scanner input = new Scanner(System.in);
int n;

while (true){
    System.out.println("Eneter a number to check if it is odd or even, then hit enter: ");
    System.out.println("Type 'STOP' to end the program");
    n = input.nextInt();

    String strN = String.valueOf(n); //convert integer to string
    if (new String(strN).equals("STOP")){ //if string is STOP, breaks
        System.out.println("Thanks for using my program");
        break;
    }

    if (n % 2 == 0){
        System.out.println(n+" is even");
        continue;
    }
    else{
        System.out.println(n+" is odd");
        continue;

I know I am missing some closing curly braces but rest assured they are all there in my actual code. Thanks.

Here is the error I am getting: Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at OddOrEven.algorithm.checker(algorithm.java:13) at OddOrEven.main.main(main.java:7)

6
  • 2
    .nextInt()? you need to prompt for a string, then, not an int. Commented Jun 2, 2015 at 21:47
  • I don't think nextInt() will check if a user enters "stop". Commented Jun 2, 2015 at 21:47
  • Yes, I tried this already. But wouldnt I have to convert the strings into integers to check if they're odd or even? Commented Jun 2, 2015 at 21:48
  • What is the exception type? Commented Jun 2, 2015 at 21:48
  • 3
    Prompt for a string, then check whether that string is "STOP" or try to cast it to int otherwise. Commented Jun 2, 2015 at 21:48

3 Answers 3

3

You've already identified the problem yourself - your Scanner reads only integers:

int n;
...
n = input.nextInt();

so it's impossible for the variable n (an int) to contain the string "STOP" (and your Scanner throws an exception anyway when you call nextInt() but it encounters a string, such as "STOP", that it cannot convert to an int).

To do this, you need to read strings from the input (probably using Scanner.nextLine()), check whether they are "STOP", and if not, only then attempt to convert them to ints using something like:

int n = Integer.parseInt(mystring)

To handle garbage input (neither STOP nor an integer), wrap the parseInt line in a try-catch block so that you can detect when the input is garbage by catching the Exception

try {
  int i = Integer.parseInt(mystring);
  // do something with the int
}
catch (NumberFormatException e) {
  // display warning message to the user instead
}

See also this related question.

Sign up to request clarification or add additional context in comments.

3 Comments

How could I check if the user typed in a garbage string, such as "dsfasdfsa" and then print out something like "Please enter a valid input"? Cause at the moment, I am doing exactly what you did, if the user didnt type in STOP, i cast it to a string. But I get many errors if I type in a garbage string.
Wrap the parseInt line in a try-catch block so that you can detect when the input is garbage by catching the Exception
Hey, I actually created a separate method that contained a try-catch block, Worked great. Thank you so much!
1

The most simple way is probably to use input.next() instead of input.nextInt(). Using input.next() will read input in as a String, then you can check if the input is equal to "QUIT", if it is not you can use Integer.parseInt to parse the Integer from the read string

Comments

1

Something like below, should work out.

NOTE: havent tested compile errors, just wrote it out(but you get a gist)

     public Scanner input = new Scanner(System.in);
    int n;

while (true){
    System.out.println("Eneter a number to check if it is odd or even, then hit enter: ");
    System.out.println("Type 'STOP' to end the program");
    n = input.next();
    Integer input;

   // String strN = String.valueOf(n); //convert integer to string
    if (strN.equals("STOP")){ //if string is STOP, breaks
        System.out.println("Thanks for using my program");
        break;
    }
    else{
        try{
    input=  Integer.parseInt(strN);

        }
        catch(Exception e)
        {
            System.out.println("Please enter a  number");
        }
    }

    if (input % 2 == 0){
        System.out.println(n+" is even");
        continue;
    }
    else{
        System.out.println(n+" is odd");
        continue;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.