为第二个测试用例抛出InputMismatchException

时间:2019-08-05 15:05:51

标签: java java.util.scanner inputmismatchexception

我的代码给出了InputMismatchException,但没有给出单个测试用例。第一次可以正常运行,第二种情况则抛出异常。它将得到的输入如下:

输入的第一行包含测试用例T的数量。每个测试用例的第一行包含两个整数N和ID1,分别表示通过总数和初始ID。接下来的N行中的每行都包含Pass的信息,该信息可以是上述两种类型之一,即:

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);
            }
        }
}

第一个和第二个测试用例的预期输出分别为13和14。 实际输出是13,并且此错误消息:

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)

1 个答案:

答案 0 :(得分:0)

问题出在方法Scanner.hasNextInt()上,因为它读取下一个输入并检查它是否是整数,而不是因为行结束而返回false。

这将导致示例输入的第6行中的数字5被读取为先前行的附加输入。因此,程序将解释输入,就像这样写:

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

如果您检查已读取的字符,而不是检查新的int,它将起作用:

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);
        }
    }
}

输出与预期的一样:

13
14