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)

Nicholas Roberts
  • 222
  • 4
  • 13

3 Answers3

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.

Community
  • 1
  • 1
DNA
  • 42,007
  • 12
  • 107
  • 146
  • Thanks for the response, very helpful! – Nicholas Roberts Jun 02 '15 at 22:18
  • 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. – Nicholas Roberts Jun 02 '15 at 22:29
  • 1
    Wrap the `parseInt` line in a `try-catch` block so that you can detect when the input is garbage by catching the [Exception](https://docs.oracle.com/javase/tutorial/essential/exceptions/) – DNA Jun 03 '15 at 07:31
  • Hey, I actually created a separate method that contained a try-catch block, Worked great. Thank you so much! – Nicholas Roberts Jun 03 '15 at 08:05
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

GregH
  • 5,125
  • 8
  • 55
  • 109
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;
    }
Polynomial Proton
  • 5,020
  • 20
  • 37