简体   繁体   English

在第 16 行从二维数组输入中获取 InputMismatchException 并且我似乎无法弄清楚为什么

[英]Getting An InputMismatchException On Line 16 From A 2D Array Input And I Can't Seem To Figure Out Why

I'm writing a program which takes a grid input and stores it as a 2d array.我正在编写一个程序,它接受网格输入并将其存储为二维数组。 However, I keep on getting a InputMismatchException error and I can't seem to find out the cause of this.但是,我不断收到 InputMismatchException 错误,我似乎无法找出原因。 Here is the code:这是代码:

import java.util.*;
public class Covid_Tracker {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int n = in.nextInt();
        int m = in.nextInt();
        char[][] A = new char[n][m];

        for (int row = 0; row < n; row++) {
            for (int col = 0; col < m; col++) {
                A[row][col]= in.next().charAt(0);;
            }
        }

        int p = in.nextInt();
        for (int i = 0; i < p; i++){
            String firstName = in.next();
            int X = in.nextInt();
            int Y = in.nextInt();
        }

        int q = in.nextInt();
        for (int i = 0; i <= q; i++){
            String firstDirection = in.nextLine();
        }

        System.out.println("Alice: infected");

    }
}

Here is the error:这是错误:

Exception in thread "main" java.util.InputMismatchException线程“main”中的异常 java.util.InputMismatchException

at java.base/java.util.Scanner.throwFor(Scanner.java:939)

at java.base/java.util.Scanner.next(Scanner.java:1594)

at java.base/java.util.Scanner.nextInt(Scanner.java:2258)

at java.base/java.util.Scanner.nextInt(Scanner.java:2212)

at Assignment_1.main(Assignment_1.java:16)

An example input would be like:输入示例如下:

5 4 5 4

XOOOO XOOOO

OOOXO欧氧

OOOOO哦哦

OOOOO哦哦

1 Chris 4 1 1 克里斯 4 1

3 3

Chris east克里斯东

Chris north克里斯北

Chris north克里斯北

(This is the part I am still working on, so not in the code above) The program will then figure out where the person has traveled, and if they have gone through an area with Covid, the program will tell me if they are infected or not. (这是我仍在研究的部分,所以不在上面的代码中)然后程序会找出此人去过的地方,如果他们经过了 Covid 地区,程序会告诉我他们是否被感染或不。

Your input should have no new lines.您的输入不应有新行。

I used the following code:我使用了以下代码:

import java.util.*;
public class Covid_Tracker {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int n = in.nextInt();
        int m = in.nextInt();
        char[][] A = new char[n][m];

        for (int row = 0; row < n; row++) {
            for (int col = 0; col < m; col++) {
                A[row][col]= in.next().charAt(0);;
            }
        }

        int p = in.nextInt();
        for (int i = 0; i < p; i++){
            String firstName = in.next();
            int X = in.nextInt();
            int Y = in.nextInt();
        }

        int q = in.nextInt();
        for (int i = 0; i <= q; i++){
            String firstDirection = in.nextLine();
        }

        System.out.println("Alice: infected");

    }
}

The input was输入是

5 4
X O O O O
O O O X O
O O O O O
O O O O O
1 Chris 4 1
3
Chris east
Chris north
Chris north

And the output was输出是

Alice:infected

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM