Vanus
Vanus

Reputation: 13

Fill list in Java with data from txt file

So I have txt file which has data about people and they are seperated with ";":

Person1;ABC;abc;123
Person2;DEF;def;456

And I have class People which has four private variables with their data and corresponding constructor.

The following code

try {
            Scanner s = new Scanner(new File("People_data.txt"));
            while (s.hasNextLine()) {

               String[] split = s.nextLine().split(";");

                // TODO: make sure the split has correct format
                people.add(new People(split[0], split[1], split[2], split[3]));

            }
        } catch (FileNotFoundException ex) {
            System.out.println("No file found");
            Logger.getLogger(City.class.getName()).log(Level.SEVERE, null, ex);
        }

returns java.lang.ArrayIndexOutOfBoundsException: 3. Now there are some similar issues in other questions, but nothing very concrete as an answer. I tried to fix the code from those answers but couldn't. I would like someone to present modified working code in this situation instead of just stating what the problem with Java is. Thanks.

Upvotes: 0

Views: 405

Answers (1)

DevilsHnd - 退した
DevilsHnd - 退した

Reputation: 9192

This is a problem that can arise should your file contain a blank line or the data line you're retrieving doesn't actually contain all the data. The first is most likely what is causing your dilemma and this is also why it's always a good idea to place what you read in from a file into a String variable and do at least one test for validity....to make sure it actually contains data. When you split a blank line which is ultimately a Null String ("") your array will only contain one Null String element instead of the 4 elements you expect for your People class therefore producing an ArrayIndexOutOfBoundsException. You're providing Indexes that simply do not exist within the split[] Array. (Note: I always avoid using method names for variable names, it reduces confusion)

Set your code to be something like this and you'll most likely be fine:

try {
    Scanner s = new Scanner(new File("People_data.txt"));
    while (s.hasNextLine()) {
       String line = s.nextLine();
       // Make sure line is not blank...
       if (!line.equals("")) {
            String[] split = line.split(";");
            // TODO: make sure the split has correct format
            if (split.length == 4) {
                people.add(new People(split[0], split[1], split[2], split[3]));
            }
            else {
                // Do what you want if data is incomplete. 
            }
       }
    }
} catch (FileNotFoundException ex) {
    System.out.println("No file found");
    Logger.getLogger(City.class.getName()).log(Level.SEVERE, null, ex);
}

In the code above we make two specific checks. First we make sure the line we read in from file actually contains data before we even process it. And secondly, we make sure we have enough data elements available within the split[] array before we apply it to People.

Upvotes: 1

Related Questions