读取文件时出错:线程“主”中的异常java.util.NoSuchElementException:未找到行

时间:2019-12-02 14:19:10

标签: java arraylist time-and-attendance

我在这里输入错误的时间和日期。我想添加特定于时间和日期的文件位置,因为我想在文件文本中列出时间和日期,因此,如果我想查看用户出席情况,可以看到它们的名称,stuno,主题,部分,时间和日期。像钟表一样?我需要考勤系统,我只想添加一些时间和日期。

这是错误代码消息

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at GroupProjP3.database.main(database.java:44)

这是我的代码:

public static void main(String[] args) throws Exception{
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
    ArrayList<String> name = new ArrayList<String>();
    ArrayList<String> studNo = new ArrayList<String>();
    ArrayList<String> section = new ArrayList<String>();
    ArrayList<String> subject = new ArrayList<String>();
    ArrayList<String> timeDate = new ArrayList<String>();
    Date td = new Date();


    File file1 = new File("C:\\Users\\WERLLY\\Desktop\\Onceng Files\\JAVA GROUP PROJECT\\name.txt");
    Scanner sca1 = new Scanner(file1);

    while(sca1.hasNextLine()) {
        String s = sca1.nextLine();
        name.add(s);
        }

    File file2 = new File("C:\\Users\\WERLLY\\Desktop\\Onceng Files\\JAVA GROUP PROJECT\\studNo.txt");
    Scanner sca2 = new Scanner(file1);

    while(sca2.hasNextLine()) {
        String s = sca1.nextLine();
        studNo.add(s);
        }

    File file3 = new File("C:\\Users\\WERLLY\\Desktop\\Onceng Files\\JAVA GROUP PROJECT\\section.txt");
    Scanner sca3 = new Scanner(file1);

    while(sca3.hasNextLine()) {
        String s = sca3.nextLine();
        section.add(s);
        }

    File file4 = new File("C:\\Users\\WERLLY\\Desktop\\Onceng Files\\JAVA GROUP PROJECT\\subject.txt");
    Scanner sca4 = new Scanner(file1);

    while(sca4.hasNextLine()) {
        String s = sca4.nextLine();
        subject.add(s);
        }

    File file5 = new File("C:\\Users\\WERLLY\\Desktop\\Onceng Files\\JAVA GROUP PROJECT\\TimeDate.txt");
    Scanner sca5 = new Scanner(file5);

    while(sca5.hasNextLine()) {
        String s = sca5.nextLine();
        timeDate.add(s);
    }

    while(true) {
        menu();
        char num = (char)buff.read();
        buff.readLine();

        switch(num){
            case '1': System.out.println("LOG IN FORM ATTENDANCE");
                System.out.print("Enter Full Name: ");
                String N = buff.readLine();
                name.add(N);
                System.out.print("Enter Student no.: ");
                String SN = buff.readLine();
                studNo.add(SN);
                System.out.print("Enter Section: ");
                String SEC = buff.readLine();
                section.add(SEC);
                System.out.print("Enter Current Subject: ");
                String SUB = buff.readLine();
                subject.add(SUB);
                System.out.println(td);
                String TD = buff.readLine();
                timeDate.add(TD);
                System.out.println("Attendance Added");
                FileWriter FW1 = new FileWriter("C:\\Users\\WERLLY\\Desktop\\Onceng Files\\JAVA GROUP PROJECT\\name.txt",true);
                FW1.write(N + System.getProperty("line.separator"));
                FW1.close();
                FileWriter FW2 = new FileWriter("C:\\Users\\WERLLY\\Desktop\\Onceng Files\\JAVA GROUP PROJECT\\studNo.txt",true);
                FW2.write(SN + System.getProperty("line.separator"));
                FW2.close();
                FileWriter FW3 = new FileWriter("C:\\Users\\WERLLY\\Desktop\\Onceng Files\\JAVA GROUP PROJECT\\section.txt",true);
                FW3.write(SEC + System.getProperty("line.separator"));
                FW3.close();
                FileWriter FW4 = new FileWriter("C:\\Users\\WERLLY\\Desktop\\Onceng Files\\JAVA GROUP PROJECT\\subject.txt",true);
                FW4.write(SUB + System.getProperty("line.separator"));
                FW4.close();
                FileWriter FW5 = new FileWriter("C:\\Users\\WERLLY\\Desktop\\Onceng Files\\JAVA GROUP PROJECT\\TimeDate.txt",true);
                FW5.write(TD + System.getProperty("line.separator"));
                FW5.close();
                break;
                    }
            }
        }

}

1 个答案:

答案 0 :(得分:1)

在读入studNo的第二个循环中,您呼叫sca1.nextLine而不是sca2.nextLine()

由于只有在sca2.hasNextLine()返回false时才能到达该行,因此将失败。