1

My code is giving InputMismatchException, but not for the single test case. It works fine for the first time and throws the exception for second case. The input it will be getting goes like:

The first line of input contains number of testcases T. The 1st line of each testcase, contains two integers, N and ID1, denoting the total number of passes and the initial ID Respectively. Each of the next N lines, contains the information of a Pass which can be either of the above two types, i.e:

1) P ID

2) B

2
3 1
P 13
P 14
B
5 1
P 12
P 13
P 14
B
B
import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
    public static void main (String[] args) {
            //code
        Scanner sc = new Scanner(System.in);
        int t = Integer.parseInt(sc.nextLine());
        for(int i = 0; i < t; i++){
                int n = sc.nextInt();
                int pre = sc.nextInt();
                int curr = 0;
                char[] arr1 = new char[n];
                int[] arr2 = new int[n];
                for(int j = 0; j < n; j++){
                    arr1[j] = sc.next().charAt(0);
                    if(sc.hasNextInt()){
                        arr2[j] = sc.nextInt();
                    }
                }
                for(int j = 0; j < n; j++){
                    if(arr1[j] == 'P'){
                    curr = arr2[j];
                        if(j > 0){
                            pre = arr2[j-1];
                        }
                    }
                    else{
                        int temp = curr;
                        curr = pre;
                        pre = temp;
                    }
                }
                System.out.println(curr);
            }
        }
}

Expected output are 13 and 14 respectively for first and second test case. Actual output is 13 and this error message:

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 GFG.main(File.java:12)
Ketan Agar
  • 11
  • 3
  • You have your `for` and `if` blocks formatted _extremely_ confusingly. They do not end at all where you expect they would, please fix the formatting so people can read this easily. – Nexevis Aug 05 '19 at 15:38

1 Answers1

0

The problem is the method Scanner.hasNextInt(), because it reads the next input and checks whether it's an integer instead of returning false because the line ended.

This causes the number 5 in the 6th line of your example input to be read as additional input for the previouse line. So the programm interpretes the input like it would be written like this:

2
3 1
P 13
P 14
B 5 //mistake is here
1
P 12
P 13
P 14
B
B

If you check the character that was read instead of checking for a new int it works:

import java.util.Scanner;

class GFG {

    public static void main(String[] args) {
        //code
        Scanner sc = new Scanner(System.in);
        int t = Integer.parseInt(sc.nextLine());
        for (int i = 0; i < t; i++) {
            int n = sc.nextInt();
            int pre = sc.nextInt();
            int curr = 0;
            char[] arr1 = new char[n];
            int[] arr2 = new int[n];
            for (int j = 0; j < n; j++) {
                arr1[j] = sc.next().charAt(0);
                /*if (sc.hasNextInt()) {//here the execution waits for the next int (which is the number 5 in the 6th line of your example
                    arr2[j] = sc.nextInt();//here it reads the value 5 which causes the error because it was expected in the next line
                }*/

                //EDIT: if you don't check for the existence of the next int, but check whether the char was 'P' it works
                if (arr1[j] == 'P') {
                    arr2[j] = sc.nextInt();
                }
            }
            for (int j = 0; j < n; j++) {
                if (arr1[j] == 'P') {
                    curr = arr2[j];
                    if (j > 0) {
                        pre = arr2[j - 1];
                    }
                }
                else {
                    int temp = curr;
                    curr = pre;
                    pre = temp;
                }
            }
            System.out.println(curr);
        }
    }
}

The output is like expected:

13
14
Tobias
  • 2,547
  • 3
  • 14
  • 29
  • Thanks for the help. But there was nothing wrong with the input. It is exactly like the way it is stated in the problem. The '5' which you kept along with B is actually the first input of the second test case. (B will not have any integer next to it, you can also see this thing in the last two lines of second test case.) – Ketan Agar Aug 05 '19 at 17:41
  • The input was correct but there was a mistake in the code that caused the programm to interprete it in a wrong way (the 5 in the wrong line) – Tobias Aug 05 '19 at 18:50