0

So I've made a multi-dimensional array lists where surnames, forenames, email.. etc should be stored and it reads this information form a .txt file. There seems to be an index out of bounds error popping up and I dont know what for.

public static void readFilesIntoArrayLists(String filename) throws IOException
{
Scanner in;
int i = 0;
in = new Scanner(filename);
String [] fileElements;

mainList = new ArrayList<ArrayList<String>>();
mainList.add(new ArrayList<String>());
mainList.add(new ArrayList<String>());
mainList.add(new ArrayList<String>());
mainList.add(new ArrayList<String>());
mainList.add(new ArrayList<String>());

while(in.hasNext())
{
  fileElements = (in.nextLine().split(","));
  mainList.get(0).add(fileElements[0]);
  mainList.get(1).add(fileElements[1]);
  mainList.get(2).add(fileElements[2]);
  mainList.get(3).add(fileElements[3]);
  mainList.get(4).add(fileElements[4]);
}
in.close();
System.out.println("Files have been read..");
}
2
  • 1
    are you sure fileElements has at least 5 elements on each line? the error is probably coming from there Commented Feb 26, 2016 at 21:25
  • 1
    Are you sure that all lines in your file have 5 elements separated by comas, do you have blank lines? Commented Feb 26, 2016 at 21:25

4 Answers 4

3

you are not checking to see if you have lines in the file which miss some of the parameters (or blank lines, or whatever).

your while loop should look something like:

while(in.hasNext())
{
  fileElements = (in.nextLine().split(","));
  if (fileElements.length == 5) {
    mainList.get(0).add(fileElements[0]);
    mainList.get(1).add(fileElements[1]);
    mainList.get(2).add(fileElements[2]);
    mainList.get(3).add(fileElements[3]);
    mainList.get(4).add(fileElements[4]);
  } else {
    // do something with error lines - log it, print to screen, or just skip them
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You are not creating a Scanner which reads the file contents. Your Scanner reads the string from the parameter filename.

To fix this, replace the current initialization

in = new Scanner(filename);

by this:

in = new Scanner(new FileInputStream(filename), "UTF-8");

Make sure to check whether your file is actually UTF-8 encoded.

You get the ArrayIndexOutOfBoundsException, since the filename does not match your desired line format.

Just two further remarks:

  • You should use the method hasNextLine in the while condition, instead of hasNext.
  • As already pointed out in the other answers, you should handle lines which do not match your expected format.

Comments

1

Always make sure that the index you are trying to access in an ArrayList are actually occupied.

A common scenario would be

while(in.hasNext())
    {
        fileElements = (in.nextLine().split(","));
        int idx = 0;
        for (; idx < fileElement.size() && idx < 5; idx++ ) {
            mainList.get(idx).add(fileElements[idx]);
        }   

        //And if you need to fill all your five arrays with data, then
        while (idx < 5) {
            mainList.get(idx).add([""]);
        }
    }

1 Comment

You didn't solve the problem, you just reversed it. in his code he has the risk of too few elements in a line, in your code - a line with more than five will throw NullPointerException on mainList.get(index).add() when index is 5 or more
0

the code seems correct. fist you must be sure that you have 5 number of elements.

modified code:

    String [] fileElements;
    mainList = new ArrayList<ArrayList<String>>();
    mainList.add(new ArrayList<String>());
    mainList.add(new ArrayList<String>());
    mainList.add(new ArrayList<String>());
    mainList.add(new ArrayList<String>());
    mainList.add(new ArrayList<String>());

int length = fileElements.getLength();
    while(in.hasNext())
    {
      fileElements = (in.nextLine().split(","));
for(int index = 0; index < length; index++)
{
      mainList.get(index).add(fileElements[index]);
}
    }

1 Comment

You didn't solve the problem, you just reversed it. in his code he has the risk of too few elements in a line, in your code - a line with more than five will throw NullPointerException on mainList.get(index) when index is 5 or more

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.