1

I encounter this problem in my javacode where my nested loop is never ending adding a value in nested value so if you request to show a output again it will add another output.

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\n");

                boolean SUBFound = 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));
                        SUBFound =true;
                    }
                }

                if(!SUBFound)
                    System.out.println("No Record of "+SB+" Subject");
                break;

And the output where you will see the problem clearly

 @#@#@#@#@ 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

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

=======================================================================================
@#@#@#@#@ 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

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

as you can see the value of for(int x=0) will remain and not reset "i think my suspect" how do i fix this? Im new in java and this is my first time i encounter this bug in loop.

CodeNoobie
  • 13
  • 4
  • Does this answer your question? [Breaking out of a for loop in Java](https://stackoverflow.com/questions/15275195/breaking-out-of-a-for-loop-in-java) – jhamon Dec 06 '19 at 08:28
  • 3
    After `SUBFound =true;` add `break` – Scary Wombat Dec 06 '19 at 08:35
  • Adding break at SUBFound =true; will break an entire loop and the given data/output will become incomplete. i just want to know what the problem of loop i dont know why the loop it double the I/O is there any method i can user for reader bufferread/ FileReader? – CodeNoobie Dec 07 '19 at 08:27

0 Answers0