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)