简体   繁体   中英

File Reader doesn't read all the data until the end

In a person.txt, we've stored the person's details. It seems like that.

John
Smith
aösldkjf
5
8645
asdfasf
0441234545
++++++
Adam
Gilchrist
ads
asf
asf
asfd
0441234546
++++++

Then we built the FileManager class to read the data out of this file. It identifies that there are two different entries. But it always read the first 8 lines and doesn't move on. Due to that the first person (eg:- John Smith) is added twice to the "LinkedList", named as AddressBook.

//File Manager Class

public class FileManager {

    public static void readFile() {

        Scanner x;

        LinkedList<String> tempList = new LinkedList<String>();

        try {
            x = new Scanner(new File("Person.txt"));

            @SuppressWarnings("unused")
            String temp = null;

            while (x.hasNext()) {
                tempList.add(x.next());
                tempList.add(x.next());
                tempList.add(x.next());
                tempList.add(x.next());
                tempList.add(x.next());
                tempList.add(x.next());
                tempList.add(x.next());
                tempList.add(x.next());

                Person person = new Person();

                person.addFilePerson(tempList);

                Main.addressBook.add(person);

            }
        } catch (Exception e) {
            System.out.println("could't find the file");
        }
    }
}

// addFilePerson method in the class Person

public void addFilePerson(LinkedList<String> list){

    vorname = list.get(0);
    nachname = list.get(1);
    strasse = list.get(2);
    hausnummer = list.get(3);
    plz = list.get(4);
    telefon = list.get(5);
    wohnort = list.get(6);
}

You're creating one LinkedList<String> and repeatedly adding to it. Move this line:

LinkedList<String> tempList = new LinkedList<String>();

into the while loop. Alternatively - and preferably, IMO - use separate properties for the different parts:

// TODO: Consider what happens if the file runs out half way through a person...
while (x.hasNext()) {
    Person person = new Person();
    person.setFirstName(x.next());
    person.setLastName(x.next());
    person.setStreet(x.next());
    person.setTown(x.next());
    person.setTelephoneNumber(x.next());
    person.setCity(x.next()); // Or whatever...

    Main.addressBook.add(person);
}

There are other options around creating a "builder" type for Person and making Person itself immutable, and you might want to create a separate Address type...

You should clear (or recreate) your list in between reading the persons from file. Otherwise, you will keep adding the same person (the first one you read) to the address book. So either recreate your temp list within the loop each time as Jon suggested, or clear it after each round:

        while (x.hasNext()) {
            tempList.add(x.next());
            ...

            Main.addressBook.add(person);

            tempList.clear();
        }

It actually moves on. This line:

person.addFilePerson(tempList);

you send the tempList as a parameter, but in addFilePerson method you always read the tempList 's first 7 entries. You should clear the tempList in every iteration of the loop.

You should use nextLine() and hasNextLine() instead of the next() and hasNext(). Scanner is context aware so default token reading behavior may not be line based.

You'd be better off doing

Person person = new Person();
Address address = new Address();
person.setAddress(address);

person.setFirstName(x.next());
person.setLastName(x.next());
address.setStreetName(x.next());
address.setHouseNumber(x.next());
address.setZipCode(x.next());
person.setPhoneNumber(x.next());
address.setCityName(x.next());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM