-2

I have error call here time and date. I'm trying to add time and date specific file location because I want to list an time and date in file text so if I want to see the user attendance I can see the name, stuno, subject, section, time and date of them. Like a clock ticker? I want attendance system and I just want to add some time and date.

this is the error code message

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

and here's my code:

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;
                    }
            }
        }

}
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Copy & Paste last line makes it unclear what is written at 44th line in the database.java file. It would be nice to have a look at repo with the code. – Malakai Dec 02 '19 at 14:24

1 Answers1

1

In your second loop reading into studNo you call sca1.nextLine instead of sca2.nextLine().

Since you'll only reach that line when sca2.hasNextLine() has returned false this will fail.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Im sorry for that i didn't notice that. i already edit that line in my orig code. my point is i want to add a command that automatically add time and date in a notepad file i assigned so if the user is log in to check his/her attendance it will automatically write the time and date in notepad. but i try to trick the java command. System.out.println(TD); String TD = buff.readLine(); timeDate.add(TD): i tought this code will work but the error occured. i just want automatically write the time and date so the user dont need to write it manually. Sorry for my english – HelpMeMahPrend Dec 03 '19 at 04:11