简体   繁体   中英

IndexOutOfBoundsException when adding to ArrayList using nested while loops

I'm trying to create a program which reads a reads a file containing the amount of people in each school district in a state and outputs the total number of people, total number of children, and total number of children below the poverty line. There are 56 states but I can only get the output for 55.

I also tried putting int i = 0 inside the first while loop but I just got a bunch of 0s.

int i = 0; int j = 1;
        while (j <= Integer.parseInt( popInfo.get(popInfo.size() - 1).getStateCode() )) {
            int tPopCt = 0, cPopCt = 0, cPovPopCt = 0;

            while (popInfo.get(i).getStateCode().equals(String.format("%02d", j))) {
                tPopCt += Integer.parseInt(popInfo.get(i).getTotalPopulation());
                cPopCt += Integer.parseInt(popInfo.get(i).getChildPopulation());
                cPovPopCt += Integer.parseInt(popInfo.get(i).getChildPovertyPopulation());
                i++;
            }
            output.add(new Output(String.format("%02d", j), tPopCt, cPopCt, cPovPopCt));
            j++;
        }

I expected the last line of the output to start with 56 (the last StateCode) however I get an error. When I set the first while loop to j < ... instead of j <= ... , I get 55 5956920 963445 157356 which is correct but does not include 56.

Please help. TY

Edit

The error message and stack trace...

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 13486 out of bounds for length 13486
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:458)
at Poverty.<init>(Poverty.java:35)
at Poverty.main(Poverty.java:70)

You should consider redesigning your program. You didn't wrote what is the type of elements in popInfo collection, so let's call it PopData . Maybe you should use one for-each loop instead of nested while loops. In this case structure of your program could look like (extract values from data variable which represents one record of data in your collection):

int globalTPopCt = 0;
int globalCPopCt = 0;
int globalCPovPopCt = 0;
for(PopData data : popInfo) {
    int state = Integer.parseInt(data.getStateCode());
    //local counters
    int localTPopCt = Integer.parseInt(data.getTotalPopulation());
    int localCPopCt = Integer.parseInt(data.getChildPopulation());
    int localCPovPopCt = Integer.parseInt(data.getChildPovertyPopulation());
    //global (cumulated) counters
    globalTPopCt += localTPopCt;
    globalCPopCt += localCPopCt;
    globalCPovPopCt += localCPovPopCt;

    output.add(new Output( ... ));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM