0

I would like to have the user input a line of text as long as x is not equal to the value of numOfContestans. When I run the code, I get an InputMismatchException. Does anyone have an idea on how to solve this error?

try {
    int numOfContestants = scan.nextInt();
    int problems = scan.nextInt();
    scan.nextLine();
    int x = 0;

    while (x != numOfContestants) {
        String input = scan.nextLine();
        x++;
    }
    System.out.println(problems);
} catch(InputMismatchException e) {
    System.out.println("Something went wrong");
}
Tom
  • 16,842
  • 17
  • 45
  • 54
kylel95
  • 139
  • 1
  • 6
  • 14
  • 2
    Most of the time, Scanner requires that you check before reading. You have to check that the Scanner `has` the thing in question. Try `hasNextLine()` before reading. – markspace Jan 02 '16 at 05:41
  • What is the input sequence for which yo are getting the exception? – YoungHobbit Jan 02 '16 at 06:12
  • @YoungHobbit I am able to enter two digits then one line of text before the error occurs. – kylel95 Jan 03 '16 at 02:28
  • You should avoid "silently" adding code to the question after an answer was provided. You can add code, but also explain in the question that you edited the code corresponding to the provided answer, but it doesn't solve your problem. – Tom Jan 03 '16 at 03:36
  • Also please provide the input you're using. – Tom Jan 03 '16 at 03:39

1 Answers1

1

You do not list the input that causes a problem. If you try this input,

3
2
line1
line2
line3

nextInt does not read the CR/LF at the end of the line. The first call to nextLine is empty. The loop runs the correct amount of times, but the first pass does not read a complete line. After reading the problems, read the next line.

int problems = scan.nextInt();        
String input = scan.nextLine();

You could also enter you data so it looks like

3
2 line1
line2
line3

Then your code works.

I could not generate an error, as long as the intergers were entered properly.

I do not know how nextLine could cause a TypeMismatchException. I have run this code and can only cause such an error if an integer is entered incorrectly. Please provide the input that causes the error.

downeyt
  • 1,206
  • 2
  • 12
  • 23
  • Thank you. I am somewhat confuse on how I should alter the code? Are you suggesting I take String input = scan.nextLine() out of the while loop? May you clarify please? Thanks. – kylel95 Jan 03 '16 at 02:27
  • Leave your code as it is and add an extra scan after you read the int. I moved the definition of String input to its new, first use. – downeyt Jan 03 '16 at 02:55
  • So once I move input out of the while loop, what remains in the loop? @downeyt – kylel95 Jan 03 '16 at 03:18
  • LEAVE YOUR CODE AS IT IS, but add an additional nextLine after the last nextInt. Since you do not need this value, you could just call nextLine without saving the result. – downeyt Jan 03 '16 at 03:23
  • I edited the initial code post above. That does not work either. Thanks for the help. @downeyt – kylel95 Jan 03 '16 at 03:31
  • You need to do something with the input. Currently, the code is not using it. Add a System.out.println(input) inside the loop to see that it works. If you need all the values later, you might put them in a List. 'problems' does not contain all the lines that were just read, it is just the count that was read earlier. – downeyt Jan 03 '16 at 03:34
  • Even though it is correct that the first `nextLine` returns an empty String, it is not clear how this causes OPs problem, when OP correctly enters two numbers (which we can only assume). – Tom Jan 03 '16 at 03:39