0

Hello i keep getting java.lang.IndexOutOfBoundsException in the line if(numList.get(x) == " " && numList.get(x+1) == " "). How do i fix this?

BufferedReader br = null;
        String row;
        String[] data = null;
        List<String> numList = new ArrayList<String>();

        BufferedReader csvReader = new BufferedReader(new FileReader("input file lab5.csv"));
        while ((row = csvReader.readLine()) != null) {
            data = row.split(",");
            // do something with the data

            for(int x=0; x<data.length; x++) {
                numList.add(data[x]);
            }
            numList.add(" ");
        }

        System.out.println("Make sure excel file is on src.\nNumbers extracted from file: "+numList);
        int n=0;

        for(int x=0; x<numList.size(); x++) {
            if(numList.get(x) == " " && numList.get(x+1) == " ") {
                n = x+1;
                break;
RDLA
  • 1
  • 2

1 Answers1

0

Add a check in if clause for last index handling (x < numList.size()-1)

if (numList.get(x) == " " && x < numList.size()-1 && numList.get(x+1) == " ") {
Bilal Siddiqui
  • 3,386
  • 1
  • 9
  • 16