0

This program takes a String followed by an integer as input and then outputs a sentence using the inputs. The program repeats until the string "quit" is typed. It appears the problem is with the nextLine() and nextInt() but I cannot wrap my head around it. I am getting the following error:

Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at LabProgram.main(LabProgram.java:8)


import java.util.Scanner;

public class LabProgram {
   public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      
      String inputStr = in.nextLine();
      int inputInt = in.nextInt();
      
      while(!inputStr.equals("quit")){
       System.out.println("Eating " + inputInt + " " + inputStr + " a day keeps you happy and healthy.");
      }  
   }
}

Thanks for the help, the following code outputs successfully:

String inputStr = "";
int inputInt;

while(!inputStr.equals("quit")){
    inputStr = in.next();
    inputInt = in.nextInt();
    in.nextLine();   //this line throws an exception but
                     //program still outputs correctly

    System.out.println("Eating " + inputInt + " " + inputStr + " a day keeps you happy and healthy.");
} 
   }
} 
  • 1
    Welcome to Stack Overflow. Sorry, but this site is not a discussion forum or programming tutorial site. Please take the [tour], visit the [help] and read [Ask] to learn how to use this site effectively. Also read [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236/18157) – Jim Garrison Jul 04 '22 at 22:23

2 Answers2

1

You are using the scanner only in the beginning and then saving the values in variables. So you only use the values in your loop. Therefore you need to let the user enter new inputs every iteration of the loop.

Move inputStr and inputInt inside your loop:

String inputStr = "";
int inputInt;

while(!inputStr.equals("quit")){
    inputStr = in.nextLine();
    inputInt = in.nextInt();

    System.out.println("Eating " + inputInt + " " + inputStr + " a day keeps you happy and healthy.");
} 

In addiditon you have to make sure to enter the right input. The first (=String) can be anything on your keyboard. The second input must be an integer, so only numbers like 1, 2, 230, -4, .... That's where your error comes from.

Japhei
  • 565
  • 3
  • 18
  • 2
    Note that you will have issues using `nextInt`, see here https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo basically after using `nextInt()` you need to consume the rest of the line using `in.nextLine();` – sorifiend Jul 04 '22 at 22:30
  • I see. When I input the string, hit enter, then input the integer the program outputs perfect. I need it to output when the user inputs the string, hits space, then the integer. – Brian Miller Jul 04 '22 at 22:52
  • Thanks for all the help. Using next() instead of nextLine() fixed my issue with the inputs. – Brian Miller Jul 04 '22 at 22:58
-1

Your program only asks for input once, so inputStr never equals quit because inputStr is set once and never changes.