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

huangapple go评论65阅读模式
英文:

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

问题

我正在编写一个程序该程序接受一个网格输入并将其存储为二维数组然而我一直在遇到InputMismatchException错误而且我似乎找不出这个错误的原因以下是代码

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

    }
}

以下是错误信息

Exception in thread "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)


示例输入可能如下

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

这部分我仍在继续努力因此不在上面的代码中然后程序将确定该人物的行程并且如果他们经过带有Covid的区域程序将告诉我他们是否被感染
英文:

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. 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 &lt; n; row++) {
for (int col = 0; col &lt; m; col++) {
A[row][col]= in.next().charAt(0);;
}
}
int p = in.nextInt();
for (int i = 0; i &lt; p; i++){
String firstName = in.next();
int X = in.nextInt();
int Y = in.nextInt();
}
int q = in.nextInt();
for (int i = 0; i &lt;= q; i++){
String firstDirection = in.nextLine();
}
System.out.println(&quot;Alice: infected&quot;);
}
}

Here is the error:

Exception in thread "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

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

(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.

答案1

得分: 0

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

    }
}

Input:

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

Output:

Alice: infected

英文:

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 &lt; n; row++) {
for (int col = 0; col &lt; m; col++) {
A[row][col]= in.next().charAt(0);;
}
}
int p = in.nextInt();
for (int i = 0; i &lt; p; i++){
String firstName = in.next();
int X = in.nextInt();
int Y = in.nextInt();
}
int q = in.nextInt();
for (int i = 0; i &lt;= q; i++){
String firstDirection = in.nextLine();
}
System.out.println(&quot;Alice: infected&quot;);
}
}

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

huangapple
  • 本文由 发表于 2020年10月5日 05:44:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64200192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定