Reputation: 13
I am trying to figure out why I get this error. My code is as follows:
ArrayList lowestQuant = new ArrayList();
for (int i = 0; i < aRes1.size(); i++) {
int var = Math.min(casesQuant.get(i), mainboardQuant.get(i));
int var2 = Math.min(var, cpuQuant.get(i));
int var3 = Math.min(var2, ramQuant.get(i));
int var4 = Math.min(var3, graphicsQuant.get(i));
lowestQuant.add(var4);
System.out.println(aRes1.get(i) +" quantity: "+lowestQuant.get(i));
}
aRes1
is an Array List of computer systems with a length of 8 computer systems.
I need to find the component with the lowest quantity in every computer system, hence the Math.min
and all the ArrayList-index
look-ups.
This code should provide the component with the lowest quantity and it does! But somehow it stops before reaching the 8th computer system. It works perfectly with the first 7
.
Can anyone tell me what the problem is?
I've looked a lot of these errors up and tried to set i=1
and .size()-1/+1
nothing helps.
Thanks in advance! Error:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 7, Size: 7
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at databasen.Database.ComputerSystems(Database.java:242)
at databasen.Database.menuLVL1(Database.java:70)
at databasen.Database.mainMenu(Database.java:61)
at databasen.Database.main(Database.java:37)
Java Result: 1
To clarify : All the array lists used are of the same length because they are made from a database. Here are some more code so you can see how the array lists are made ::
ArrayList aRes1 = new ArrayList();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT catchyname FROM computersystems");
System.out.printf("Computer sytems:\n");
while (rs.next()) {
String answer = rs.getString("catchyname");
aRes1.add(answer);
}
// Cases
ArrayList aRes2 = new ArrayList();
rs = st.executeQuery("SELECT cases FROM computersystems");
while (rs.next()) {
String answer = rs.getString("cases");
aRes2.add(answer);
}
ArrayList<Integer> casesQuant = new ArrayList<Integer>();
for (int i=0;i<aRes2.size();i++){
ResultSet rs2 = st.executeQuery("SELECT currentquantity FROM components "
+ "WHERE name ="+"'"+aRes2.get(i)+"'");
while (rs2.next()) {
int answer2 = rs2.getInt("currentquantity");
casesQuant.add(answer2);
}
}
Upvotes: 1
Views: 10837
Reputation: 8961
you are working with multiple arrays that don't all have the same size, but using an index from one array on all of the arrays. this is the cause of the exception.
Upvotes: 0
Reputation: 114837
line 242 is "
int var = Math.min(casesQuant.get(i), mainboardQuant.get(i));
"
Add the following line before that one:
System.out.printf("Expected: #s, Cases: #s, Mainboards: #s#n", aRes1.size(),
casesQuant.get(i), mainboardQuant.get(i));
One number should be less then the expected value.
Upvotes: 0
Reputation: 15
Java starts at index 0 and goes up. A size of 7 means an index of 6. Change: (int i = 0; i < aRes1.size(); i++) To: (int i = 0; i < aRes1.size()-1; i++) Subtracting one will fix the problem.
Upvotes: 1
Reputation: 146
Any one of the Array Lists has 7 elements . Try checking line number 547 and 322 . you will get an idea which one .
Upvotes: 1