Don’t think there’s much to say, here’s my code
for (int i = 0; i < numFactors.size(); i++) {
for (int j = 0; i < denFactors.size(); j++) {
if (numFactors.get(i) == denFactors.get(j)) {
commonFactors.add(numFactors.get(i));
}
}
}
And I get this error
Exception in thread "Thread-4" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com.creatif.cubes.Fraction.simplify(Fraction.java:64)
Where line 64 is at the if statement. I can’t for my life find the problem here.
Your inner for loop reads:
It’s not clear that
jwill stop incrementing before it reachesdenFactors.size(), thus the index out of bounds exception. Did you mean: