英文:
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 < 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
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 < 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论