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);
}
}
Any one of the Array Lists has 7 elements . Try checking line number 547 and 322 . you will get an idea which one .