javacsvexceptionbufferedreader

Array Index out of bounds when reading from CSV file


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?


Solution

  • 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 ){