java.util.NoSuchElementException 错误

标签 java readfile

我尝试从文本文件中读取整行并以不同的方式显示它们。例如,

    123456J;Gabriel;12/12/1994;67;67;89;

但是控制台的结果是这样的:

    123456J Gabriel 72(which is average of three numbers)

这是我的代码:

    public class Student{

String adminNo;
String name;
GregorianCalendar birthDate;
int test1,test2,test3;

public Student(String adminNo,String name,String birthDate,int test1, int test2, int test3){
    this.adminNo = adminNo;
    this.name = name;
    this.birthDate = MyCalendar.convertDate(birthDate);
    this.test1 = test1;
    this.test2 = test2;
    this.test3 = test3;
}

public Student(String studentRecord){
    String strBirthDate;
    Scanner sc = new Scanner(studentRecord);
    sc.useDelimiter(";");
    adminNo = sc.next();
    name = sc.next();
    strBirthDate = sc.next();
    birthDate = MyCalendar.convertDate(strBirthDate.toString());
    test1 = sc.nextInt();
    test2 = sc.nextInt();
    test3 = sc.nextInt();
}

public int getAverage(){ 
    return (( test1 + test2 + test3 ) / 3 ) ;
}

public String toString(){
    return (adminNo + " " + name + " " + getAverage());
}

public static void main(String [] args){

    Student s = new Student ("121212A", "Tan Ah Bee", "12/12/92", 67, 72, 79);
    System.out.println(s);

    String fileName = "student.txt";
    try{
        FileReader fr = new FileReader(fileName);
        Scanner sc = new Scanner(fr);

        while(sc.hasNextLine()){
            Student stud = new Student(sc.nextLine());
            System.out.println(stud.toString());
        }

        fr.close();
    }catch(FileNotFoundException exception){
        System.out.println("File " + fileName + " was not found");
    }catch(IOException exception){
        System.out.println(exception);
    }
}

错误:

    Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Student.<init>(Student.java:24)
at Student.main(Student.java:52)

但我收到了 java.util.NoSuchElementException 错误。我错过了什么吗?它现在可以正常工作,但突然出现错误。我不知道为什么。

如有任何帮助,我们将不胜感激。提前致谢。

最佳答案

您可以将 Student 构造函数修改为如下所示,以检查发生异常时的行。

public Student(String studentRecord){
    String strBirthDate;
    Scanner sc = new Scanner(studentRecord);
    sc.useDelimiter(";");

    try {
         adminNo = sc.next();
         name = sc.next();
         strBirthDate = sc.next();
         birthDate = MyCalendar.convertDate(strBirthDate.toString());
         test1 = sc.nextInt();
         test2 = sc.nextInt();
         test3 = sc.nextInt();
    } catch (NoSuchElementException exception)
        System.out.println("NoSuchElementException, the line was: " + studentRecord);
    }
}

关于java.util.NoSuchElementException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16098114/

相关文章:

java - ParameterizedBeanPropertyRowMapper 的性能影响可以忽略不计吗?

java.rmi.server.codebase 在 linux 上不工作

c++ - ReadFile() 返回 1, "lpNumberOfBytesRead"设置为 0

json - 当我从 txt 中读取时,Dict 的键没有得到认可 - Julia

node.js - 如何在 Angular 7 中创建使用 FS 模块列出目录中文件的服务?

python - 逐批读取文件中的多行

Javafx无法加载新添加的图像

java - 设置组件外观

java - "class, interface, or enum expected"Java语法错误的原因?

javascript - 是否可以在 indesign 脚本中读取 MS Document 或 Excel?