[英]Getting error message"Exception in thread "main" java.util.InputMismatchException"
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int iteam = sc.nextInt();
Student[] students = new Student[iteam];
int id;
String name;
String city, s_city;
double marks, s_marks;
for (int i = 0; i < iteam; i++) {
id = sc.nextInt();
name = sc.nextLine();
city = sc.nextLine();
marks = sc.nextDouble();
students[i] = new Student(id, name, city, marks);
}
}
I got an error " Exception in thread "main" java.util.InputMismatchException
我收到错误“
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)"
I also, try using next() but the same error exists.我也尝试使用 next() 但存在相同的错误。 and I also import java.util.Scanner.
我还导入了 java.util.Scanner。
There's not a lot of info here, but I see a problem that could definitely be causing your problem.这里没有很多信息,但我看到一个问题肯定会导致您的问题。
When you read a numeric value from a line, and expect the user to have hit return to submit that value, you have to consume the end of line before you can move on.当您从一行中读取一个数值,并期望用户按回车键提交该值时,您必须先消耗行尾,然后才能继续。 The Scanner doesn't do this automatically.
扫描仪不会自动执行此操作。 To have the user enter 4 values, hitting return after each entry, your input loop should look like this:
要让用户输入 4 个值,在每次输入后按回车键,您的输入循环应如下所示:
for (int i = 0; i < iteam; i++) {
id = sc.nextInt(); sc.nextLine();
name = sc.nextLine();
city = sc.nextLine();
marks = sc.nextDouble(); sc.nextLine();
students[i] = new Student(id, name, city, marks);
}
The previous solution was not complytly right.以前的解决方案并不完全正确。 After a little debug-session i figured the following solution out:
经过一些调试会话后,我想出了以下解决方案:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int iteam;
iteam = sc.nextInt();
Student[] students = new Student[iteam];
int id;
String name;
String city;
double marks;
for (int i = 0; i < iteam; i++) {
id = sc.nextInt();
name = sc.next();
city = sc.next();
marks = sc.nextDouble();
students[i] = new Student(id, name, city, marks);
}
}
A little explanation for you: The methods next()
, nextInt()
and nextDouble()
scans the next token as a input value.给你一点解释:方法
next()
、 nextInt()
和nextDouble()
扫描下一个标记作为输入值。 But most important the Return doesn't count, because your bash interpreted this as your input.但最重要的是 Return 不算数,因为您的 bash 将其解释为您的输入。
Also, your keyboard input is localized interpreted, especially if you want to read a double.此外,您的键盘输入是本地化解释的,特别是如果您想读取双精度。 For example, the English separator is written as DOT and in German as a COMMA .
例如,英语分隔符写为DOT ,德语写为COMMA 。
You need to call sc.nextLine()
after reading the first int from the input as you are reading 4 values.当您读取 4 个值时,您需要在从输入中读取第一个 int 后调用
sc.nextLine()
。 Scanner#nextInt()
only reads the integer, but does not read the newline "\\n"
; Scanner#nextInt()
只读取整数,但不读取换行符"\\n"
; you will need to consume the newline using Scanner#nextLine
.您将需要使用
Scanner#nextLine
使用换行符。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int iteam = sc.nextInt();
Student[] students = new Student[iteam];
int id;
String name;
String city, s_city;
double marks, s_marks;
for (int i = 0; i < iteam; i++) {
id = sc.nextInt();
sc.nextLine();//call next line to prevent exception
name = sc.nextLine();
city = sc.nextLine();
marks = sc.nextDouble();
students[i] = new Student(id, name, city, marks);
}
}
Full Working Example:完整的工作示例:
import java.util.Scanner;
public class MyClass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int iteam = sc.nextInt();
Student[] students = new Student[iteam];
int id;
String name;
String city, s_city;
double marks, s_marks;
for (int i = 0; i < iteam; i++) {
id = sc.nextInt();
sc.nextLine();
name = sc.nextLine();
city = sc.nextLine();
marks = sc.nextDouble();
students[i] = new Student(id, name, city, marks);
}
for(Student s: students){
System.out.println(s);
}
}
private static class Student{
private int id;
private String name;
private String city;
private double marks;
public Student(int id, String name, String city, double marks){
this.id = id;
this.name = name;
this.city = city;
this.marks = marks;
}
public String toString(){
return "[Student] Id: "+this.id+", Name: "+this.name+", City: "+this.city+", Mark: "+this.marks;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.