0

I'm trying to read a CSV file using a BufferedReader, but for some reason I get an out of bounds exception after 7 rows. I tried this exact algo on another CSV file (30 rows) and it worked fine. Here is the CSV file in question.

    String spellPath = "file path is here";

    FileReader y = new FileReader(spellPath);
    BufferedReader x = new BufferedReader(y);
    ArrayList<Card> ArrayList = new ArrayList<Card>( );   //dynamic data type

    for( String p = x.readLine(); p != null ; p = x.readLine()){

        String [] stArray = p.split(",");
        ArrayList.add(new Card( stArray[1], stArray[2])); //new card with name and desc only

    }

    System.out.println(ArrayList.toString());

Is the problem with the file or is it with the algorithm?

2
  • 1
    use this type of loopwhile((String s = br.readLine()) != null) instead of for loop Commented Feb 18, 2015 at 14:10
  • At least one of your lines doesn't have a comma in it. It looks like you have a newline in the middle of one of your rows. Commented Feb 18, 2015 at 14:12

6 Answers 6

3

Your issue here is the 2 consecutive calls to p=x.readLine()

for( String p = x.readLine(); p != null ; p = x.readLine()){
    ...
}

Due to this, 2 lines are read and only 1 is checked for null

You need to change the loop to

while (true) {
    String p= x.readLine();
    if (p == null) break;

    ...
}
Sign up to request clarification or add additional context in comments.

Comments

2

There is one line "gains 500 ATK and DEF for each Spell Card you have on the field." that do not contain any ,. So stArray[] has a length of 1.

Other thing: Java arrays are zero base.

And for( String p = x.readLine(); p != null ; p = x.readLine()){ should be while ((String p = x.readLine())!= null ){

Comments

1

Try this.

while(x.readLine() != null){
---`enter code here`
}

Comments

1
while((String p = x.readLine()) != null){

    String [] stArray = p.split(",");
    ArrayList.add(new Card( stArray[0], stArray[1])); //new card with name and desc only

}

System.out.println(ArrayList.toString());

this should works

Comments

1

You are calling x.readLine()twice in the loop. Hence you are skipping lines while reading.

Better way would be to use CSVReader rather than the buffered reader.

CSVReader reader = new CSVReader(new FileReader(fName), ',','"','|');
    List content = reader.readAll();//do not use this if CSV file is large
    String[] row = null;

    for (Object object : content) {
        row = (String[]) object;
        row = Arrays.toString(row).split(",");
        //now you have a row array with length equal to number of columns
    }

Here is the link to get CSVReader - CSVReader Download

Comments

0

Error is throwing here

String [] stArray = p.split(",");
 ArrayList.add(new Card( stArray[1], stArray[2]));

Add this condition and check

String [] stArray = p.split(",");
ArrayList.add(new Card( stArray[0], stArray[1]));

1 Comment

The answer is the right to life, as sets the right direction.

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.