Bobby Edwards
Bobby Edwards

Reputation: 1

Why am I getting more lines added to my arraylist from a file I am reading?

The file only consists of 4 lines and and when I add my nextLine() in the for loop, I get an error "Exception in thread "main" java.util.NoSuchElementException: No line found" but if I put it before the for loop or if I add it before the for loop it stores 8 classes.

Moving the nextLine around before and after the for loop and also in it, as well as changing what x equals in the for loop

public static void main(String args[]) {
        WLS client = new WLS();
        print("WLS Status: " + client.status());
        client.checkFileSize();
        print("Login Lines: " + loginLines);
        client.getLogins();
        print("Logins Stored: " + loginList.size());
    }

public void getLogins() {
            String[] parsedLogin;
            if(active) {
                try {
                    print("Gathering info from file: " + inputFile + ".txt");
                    file = new Scanner(new File(inputFile + ".txt"));
                    while(file.hasNextLine()) {
                        parsedLogin = file.nextLine().split(" ");
                        file.nextLine();
                        for(int x = 0; x < logins.length; x++) {
                            logins[x] = new Logins(parsedLogin[0], parsedLogin[1], parsedLogin[2]);
                            loginList.add(logins[x]);//#TODO Storing 8 for some reason file.nextLine(); is causing a breakage NEED TO FIX!!
                            file.nextLine();
                        }
                    }
                } catch(FileNotFoundException e) { print("File not found"); }
            } else { print("Activate WLS to proceed"); }
        }

I expect the output to be:

WLS Activated
WLS Status: true
Enter database name: (Do NOT include extension! i.e. '.txt')
database
Login Lines: 4
Gathering info from file: database.txt
Logins Stored: 4

But the actual output is:

WLS Activated
WLS Status: true
Enter database name: (Do NOT include extension! i.e. '.txt')
database
Login Lines: 4
Gathering info from file: database.txt
Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at Main$WLS.getLogins(Main.java:59)
    at Main.main(Main.java:17)

Upvotes: 0

Views: 32

Answers (1)

Sharon Ben Asher
Sharon Ben Asher

Reputation: 14373

you placed the reading of next line inside the wrong loop. you have two loops: first to read the lines and the second on the split line. the first loop determines the sequence of reading lines

file = new Scanner(new File(inputFile + ".txt"));
while(file.hasNextLine()) {
    parsedLogin = file.nextLine().split(" ");  // only place to read line
    for(int x = 0; x < logins.length; x++) {
        logins[x] = new Logins(parsedLogin[0], parsedLogin[1], parsedLogin[2]);
        loginList.add(logins[x]);//#TODO Storing 8 for some reason file.nextLine(); is causing a breakage NEED TO FIX!!
    }
}

Upvotes: 1

Related Questions