获取错误消息“线程“主”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);


    }

}

我有一个错误“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)"

我也尝试使用 next() 但存在相同的错误。我还导入了 java.util.Scanner。


阿波罗的战车
浏览 110回答 3
3回答

哆啦的时光机

这里没有很多信息,但我看到一个问题肯定会导致您的问题。当您从一行中读取一个数值,并期望用户点击返回来提交该值时,您必须先消耗行尾才能继续。扫描仪不会自动执行此操作。要让用户输入 4 个值,在每次输入后按回车键,您的输入循环应如下所示:for (int i = 0; i < iteam; i++) {&nbsp; &nbsp; id = sc.nextInt(); sc.nextLine();&nbsp; &nbsp; name = sc.nextLine();&nbsp; &nbsp; city = sc.nextLine();&nbsp; &nbsp; marks = sc.nextDouble(); sc.nextLine();&nbsp; &nbsp; students[i] = new Student(id, name, city, marks);}

慕婉清6462132

当您读取 4 个值时,您需要sc.nextLine()在从输入中读取第一个 int 后调用。 Scanner#nextInt()只读取整数,不读取换行符"\n";您将需要使用Scanner#nextLine.public static void main(String[] args) {&nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; int iteam = sc.nextInt();&nbsp; &nbsp; Student[] students = new Student[iteam];&nbsp; &nbsp; int id;&nbsp; &nbsp; String name;&nbsp; &nbsp; String city, s_city;&nbsp; &nbsp; double marks, s_marks;&nbsp; &nbsp; for (int i = 0; i < iteam; i++) {&nbsp; &nbsp; &nbsp; &nbsp; id = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; sc.nextLine();//call next line to prevent exception&nbsp; &nbsp; &nbsp; &nbsp; name = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; city = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; marks = sc.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; students[i] = new Student(id, name, city, marks);&nbsp; &nbsp; }}完整的工作示例:import java.util.Scanner;public class MyClass {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; int iteam = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; Student[] students = new Student[iteam];&nbsp; &nbsp; &nbsp; &nbsp; int id;&nbsp; &nbsp; &nbsp; &nbsp; String name;&nbsp; &nbsp; &nbsp; &nbsp; String city, s_city;&nbsp; &nbsp; &nbsp; &nbsp; double marks, s_marks;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < iteam; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; city = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; marks = sc.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; students[i] = new Student(id, name, city, marks);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for(Student s: students){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(s);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static class Student{&nbsp; &nbsp; &nbsp; &nbsp; private int id;&nbsp; &nbsp; &nbsp; &nbsp; private String name;&nbsp; &nbsp; &nbsp; &nbsp; private String city;&nbsp; &nbsp; &nbsp; &nbsp; private double marks;&nbsp; &nbsp; &nbsp; &nbsp; public Student(int id, String name, String city, double marks){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.city = city;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.marks = marks;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public String toString(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "[Student] Id: "+this.id+", Name: "+this.name+", City: "+this.city+", Mark: "+this.marks;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

心有法竹

以前的解决方案并不完全正确。经过一点调试会话后,我想出了以下解决方案:public static void main(String[] args) {&nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; int iteam;&nbsp; &nbsp; iteam = sc.nextInt();&nbsp; &nbsp; Student[] students = new Student[iteam];&nbsp; &nbsp; int id;&nbsp; &nbsp; String name;&nbsp; &nbsp; String city;&nbsp; &nbsp; double marks;&nbsp; &nbsp; for (int i = 0; i < iteam; i++) {&nbsp; &nbsp; &nbsp; &nbsp; id = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; name = sc.next();&nbsp; &nbsp; &nbsp; &nbsp; city = sc.next();&nbsp; &nbsp; &nbsp; &nbsp; marks = sc.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; students[i] = new Student(id, name, city, marks);&nbsp; &nbsp; }}给你一点解释:方法next(),nextInt()并nextDouble()扫描下一个标记作为输入值。但最重要的是 Return 不算数,因为您的 bash 将其解释为您的输入。此外,您的键盘输入是本地化解释的,特别是如果您想读取双精度。例如,英文分隔符写为DOT,而德文则写为COMMA。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java