2

Here's my code:

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int n = in.nextInt();
            int m = in.nextInt();
            String s = in.nextLine();
            for (int i = 0; i < m; i++) {
                int pos = in.nextInt();    // <- Line 14
                char c = (char)in.nextByte();
                s = s.substring(0, pos) + c + (pos + 1 < n ? s.substring(pos + 1) : "");
                System.out.println(f(s));
            }
        }
    }
}

After I input

10 3 .b..bz....

The program throws an exception immediately:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at Test.main(Test.java:14)

I don't even have chance to input pos and c.

Could anybody tell me what is wrong in this snippet? Thanks in advance.

Madhawa Priyashantha
  • 9,208
  • 7
  • 28
  • 58
user2916610
  • 755
  • 5
  • 11
  • *Thats because the Scanner#nextInt method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner#nextInt* – Yassin Hajaj Mar 29 '16 at 13:34

1 Answers1

3
int pos = in.nextInt();

For the input of b which cannot be converted to an integer. That is why the exception is thrown.

ozOli
  • 1,294
  • 1
  • 16
  • 22