0

I'm working on this code which takes an integer as a test case then takes a string and an integer for each test case But I keep getting this exception:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at OverSizedPancakeFlipper.main(OverSizedPancakeFlipper.java:18)

My code basically looks like this:

Scanner userInput = new Scanner(System.in);
int T=userInput.nextInt();

userInput.nextLine();
while(T-->0){
    String S=userInput.nextLine();
    char[] ch = S.toCharArray();
    int K=userInput.nextInt();
    //code does work here
}

Do let me know if you need any other information and thanks for all the help.

  • What is the input? – BackSlash Apr 08 '17 at 13:40
  • @BackSlash The first line of the input gives the number of test cases, T. T test cases follow. Each consists of one line with a string S and an integer K. The string consists of a series of + and - symbols. – Maharsh Mangal Apr 08 '17 at 13:51
  • I meant, what are you providing as input? Provide us with sample data which demonstrate the issue, so that we can tell you what's going on – BackSlash Apr 08 '17 at 14:23
  • Possible duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Patrick Parker Apr 08 '17 at 14:28

3 Answers3

1

Scanner userInput = new Scanner(System.in);

do{
    String s=userInput.nextLine();
    char[] ch = s.toCharArray();
    int k=userInput.nextInt();
     //code does work here
}while(k==0);

you can use this.....

Avreen
  • 21
  • 6
  • didn't solve my problem..can you explain why you decided to go for a do while loop? – Maharsh Mangal Apr 08 '17 at 14:01
  • So, basically it will prompt you to enter the string while k=0;it gives you the chance to enter the string before checking the further conditions – Avreen Apr 08 '17 at 16:16
0

Change your code to

while(variableName==0){
  ...
}

when writing a while loop: you want to make sure that the loop will stop at current point, so it won't go forever.

MarianD
  • 9,720
  • 8
  • 27
  • 44
Linan
  • 1
  • 2
0

Change this line:

int K=userInput.nextInt();

To this:

int K=Integer.parseInt(userInput.nextLine());

Of course you will need to handle NumberFormatException.

For an explanation of this change, see duplicate question Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

In short, the problem is that you forgot to read the newline after the integer. So then nextLine will take the empty string and consume the newline. Then nextInt will fail because it is positioned at invalid content.

Community
  • 1
  • 1
Patrick Parker
  • 4,381
  • 3
  • 15
  • 43
  • To avoid the behaviour, you should just call `userInput.nextLine()` before the `nextInt()`. No need to parse the int yourself, just ignore a line, as the answer you linked suggests too. – BackSlash Apr 08 '17 at 14:45
  • @BackSlash I am aware that there are multiple ways to solve it, yes. But the linked answer said " it would be even better" to do this way. P.S. calling nextLine before nextInt as you suggest would not work. Instead you would need to call it after nextInt. Personally, it seems a bit hackish to me. You are potentially ignoring data on the line that followed the integer. – Patrick Parker Apr 08 '17 at 16:37