0

I am trying to read numbers from an excel file and putting them in arrays but I am getting an out of bounds exception ... the excel file has 2 columns and 32 rows per column... the numbers in the first column(which are 5 digits long) are to be inserted in an array named allInputs while those of the second column (3 digits long) are to be inserted in an array named allTargets ... the following is my code;

String[] allInputs = new String[32];
    String[] allTargets= new String[32];

     //Read the values from csv file 

    //Input file which needs to be parsed
    String fileToParse = "C:\\Users\\ROBMARYAN\\Documents\\UOM\\Bsc IT Comp & Business\\3rd Yr\\Business Intelligence\\NeuralNetAssignment\\src\\inputs.csv";
    BufferedReader fileReader = null;

    //Delimiter used in CSV file
    final String DELIMITER = ",";
    try
    {
        String line = "";
        //Create the file reader
        fileReader = new BufferedReader(new FileReader(fileToParse));

        //Read the file line by line
        int icount=0; //determine where to store the input
        int tcount=0; //determine where to store the target
        while ((line = fileReader.readLine()) != null)
        {
            //Get all tokens available in line
            String[] tokens = line.split(DELIMITER);
            for(String token : tokens)
            {
                //Print all tokens
                if (token.length() == 5)
                {
                        ***allInputs[icount]=token***;
                        System.out.println("Stored in all inputs:"+allInputs[icount]);
                        icount++;
                }
                else
                {
                    allTargets[tcount]=token;
                    System.out.println("Stored in all targets:"+allTargets[tcount]);
                    tcount++;
                }
                System.out.println("iCOUNT:"+icount);
                System.out.println("tCOUNT:"+icount);
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally
    {
        try {
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

on the line where allInputs[icount]=token , I am getting the following error;

java.lang.ArrayIndexOutOfBoundsException: 32

Thanks in advance!

1 Answer 1

1

That line of code runs once per cell, if the cell's content length is 5. There could be up to 64 of these, since your spreadsheet is 32 x 2. But you're trying to put them into an array of length 32, which of course overspills its bounds.

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

2 Comments

in the excel i know that I've put 32 numbers which are 5 digits long and that is not gonna change so I think that it shouldn't over spill the bound...
OK, then let's see the file so we can reproduce and debug this.

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.