1. Member
Join Date
Nov 2010
Posts
2
Rep Power
0

## need help with factor quadratic...

Java Code:
//it displays the error java.util.ConcurrentModificationException when it runs. i believe something is wrong with the getQuadraticFactors or getIntegerFactors. please give me advices...

import java.util.ArrayList;

public static void main(String[] args) {
eqn.setABC(1, -2, 3);
System.out.println(eqn.toString());

eqn2.setABC(1, -6, 9);
System.out.println(eqn2.toString());

}

private int coeA;
private int coeB;
private int coeC;
ArrayList<Integer> listA = new ArrayList<Integer>();
ArrayList<Integer> listB = new ArrayList<Integer>();

public void setABC(int a, int b, int c) {
coeA = a;
coeB = b;
coeC = c;

}

public String toString() {
return coeA + "x^2" + " + " + coeB + "x" + " + " + coeC;
}

public ArrayList<Integer> getIntegerFactors(int n) {
for (int a = 1; a <= n; a++) {
if (a == 1 || a == n || n % a == 0) {
}
}
return listA;

}

public ArrayList<Integer> getIntegerFactors2(int c) {
for (int a = 1; a <= c; a++) {
if (a == 1 || a == c || c % a == 0) {

}
}
return listB;
}

String str = "";
for (Integer P : getIntegerFactors(coeA)) {
for (Integer Q : getIntegerFactors2(coeC)) {
for (Integer R : getIntegerFactors(coeA)) {
for (Integer S : getIntegerFactors(coeC)) {
if (P*S + Q*R== coeB
&& P*R == coeA
&& Q*S== coeC) {

str = "(" + P + "x +" + Q + ")" + "(" + R + "x + " + S + ")";
return str;
}
}
}
}
}

return "PRIME";

}
}
Last edited by frostkarrotor; 11-29-2010 at 02:32 AM. Reason: Moderator Edit: code tags added

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
Do listA and listB have to be instance variables? If not declare them in the methods that create and return them.

The inner for loops are altering the contents of listA while the outer ones are still iterating over it, which doesn't make a lot of sense.

3. Member
Join Date
Nov 2010
Posts
2
Rep Power
0
declare them in the quadraticFactors or getIntegerFactors? when u say the outer ones are still iterating, do you mean the one in getQuadraticFactor or getIntegerFactors?

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
listA and listB are created and returned from the getIntegerFactors() and getIntegerFactors2() methods. What I am suggesting is that you not use them as instance variables.

The nested for loops occur in the getQuadraticFactors() method. Do you see how the R and S loops will be altering the contents of listA while P loop is still trying to iterate over it?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•