Search code examples
javajconsole

Nested Loop Problem/ Looped 2 times and also running default in switch case


I Have a problem about my nested loop. Every time i request to show a specific data type it will show it but the problem is. it always double the output. How to i stop it? any also the Else showed too. i search all nested solution overhere but i don't see a similar code solution in my code. or any similarity answer for my code.

NOTE: Im new in Programming JAVA and this is my first Project so please be patient on me and explain everything what i did wrong and where i get this error/bug :) Thank you in advance

Here the code

case '2':
            System.out.print("Enter Subject: ");
            SB = buff.readLine();

            File readSUB = new File("E:\\Onceng Files\\JAVA GROUP PROJECT\\subject.txt");
            File readN = new File("E:\\Onceng Files\\JAVA GROUP PROJECT\\name.txt");
            File readSN = new File ("E:\\Onceng Files\\JAVA GROUP PROJECT\\studNo.txt");
            File readSEC = new File("E:\\Onceng Files\\JAVA GROUP PROJECT\\section.txt");
            File readTD = new File ("E:\\Onceng Files\\JAVA GROUP PROJECT\\TimeDate.txt");
            Scanner scaSUB = new Scanner(readSUB);
            Scanner scaN = new Scanner(readN);
            Scanner scaSN = new Scanner(readSN);
            Scanner scaSEC = new Scanner(readSEC);
            Scanner scaT = new Scanner(readTD);
            while(scaSUB.hasNextLine()) {
                String s = scaSUB.nextLine();
                SUBJECT.add(s);
            }

            while(scaN.hasNextLine()) {
                String s = scaN.nextLine();
                NAME.add(s);
            }

            while(scaSN.hasNextLine()) {
                String s = scaSN.nextLine();
                STUDNO.add(s);
            }

            while(scaSEC.hasNextLine()) {
                String s = scaSEC.nextLine();
                SECTION.add(s);
            }

            while(scaT.hasNextLine()) {
                String s = scaT.nextLine();
                TIME.add(s);
            }

            System.out.println("Attendance List by SUBJECT: "+SB);
            System.out.println("SUBJECT\t\tNAME\t\t\tSTUDENT NO.\t\tTIME & DATE");
            for(int x=0;x<SUBJECT.size();x++) {
                if(SB.equals(SUBJECT.get(x))) {
                    System.out.println(SUBJECT.get(x)+"\t\t"+NAME.get(x)+"\t\t"+STUDNO.get(x)+"\t\t"+TIME.get(x));
                } else
                    System.out.println("No Record of "+SB+" Subject");
            }

            break;

and here the OUTPUT

    @#@#@#@#@ MENU @#@#@#@#@
[1] LOGIN ATTENDANCE
[2] SHOW ATTENDANCE BY SUBJECT
[3] SHOW ATTENDANCE BY SECTION
[4] SHOW ALL RECORD
Input Number: 2
Enter Subject: JAVA
Attendance List by SUBJECT: JAVA
SUBJECT     NAME            STUDENT NO.     TIME & DATE
No Record of JAVA Subject
No Record of JAVA Subject
No Record of JAVA Subject
JAVA        Saludaga Joshua     19-01297        Tue, 12/03/2019 18:02
JAVA        Pulano Hardhie      19-00900        Tue, 12/03/2019 18:02
JAVA        Tatoy Cherrylyn     19-00751        Tue, 12/03/2019 18:02

Solution

  •         for(int x=0;x<SUBJECT.size();x++) {
                if(SB.equals(SUBJECT.get(x))) {
                    System.out.println(SUBJECT.get(x)+"\t\t"+NAME.get(x)+"\t\t"+STUDNO.get(x)+"\t\t"+TIME.get(x));
                } else
                    System.out.println("No Record of "+SB+" Subject");
            }
    

    above should be written as.

    boolean found = false;
            for(int x=0;x<SUBJECT.size();x++) {
                if(SB.equals(SUBJECT.get(x))) {
                    System.out.println(SUBJECT.get(x)+"\t\t"+NAME.get(x)+"\t\t"+STUDNO.get(x)+"\t\t"+TIME.get(x));
    found =true;
                }
    
            }
    if(!found)
      System.out.println("No Record of "+SB+" Subject");
    

    Hope this solves your problem.