# Thread: Factor listing issues

1. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## Factor comparing issues

OUTDATED, PLEASE DO NOT RESPOND
new post: http://www.java-forums.org/new-java/...tml#post242317
So my problem is... When my two numbers are 10 and 15,it outputs 0 and it lists out all the factors of 10 then errors... Here's the output:
0
1
2
5
10
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Greatest_Common_Factor.main(Greatest_Common_Factor .java:30)
(Factors of 15 supposed to be here)

Java Code:
```
public class Greatest_Common_Factor {
public static void main (String args[]){
int number = 10;
int number2 = 15;
int factor1Length = 0;
int itterations = 0;
int itterations2 = 0;
int[] factors1;
int[] factors2;
factors1 = new int[1 + getFactor1Length(number)]; //Gets all factors of number
factors2 = new int[1 + getFactor2Length(number2)]; // Gets all factors of number 2

for(int x=1; x<=(number); x++){ //puts all factors of num1 into factors1 array
if(number % x == 0){
itterations++;
factors1[itterations] = x;
}//end if
}//end for

for(int y=1; y<=(number2); y++){
if(number2 % y == 0){
itterations2++;
factors2[itterations2] = y;
}//end if
}//end for

//Print all factors of everything
for(int a=0; a<=factors1.length; a++){
System.out.println(factors1[a]);
}
for(int b=0; b<=factors2.length; b++){
System.out.println(factors1[b]);
}

}//end main

//**********************Get Factor 2 Length****************************
private static int getFactor2Length(int number2) {
int factor2Length = 0;
for(int i=1; i<=(number2); i++){
if(number2 % i == 0){
factor2Length++;
}//end if
}//end for
return factor2Length;
}//end method

//**********************Get Factor Length****************************
private static int getFactor1Length(int number) {
int factor1Length = 0;
for(int i=1; i<=(number); i++){
if(number % i == 0){
factor1Length++;
}//end if
}//end for
return factor1Length;
}//end method

}//end class```
Last edited by skaterboy987; 10-30-2011 at 03:17 AM.

2. ## Re: Factor listing issues

Why do you have 2 methods (getFactorLength) that do the same thing?

3. ## Re: Factor listing issues

More duplication.

The 2 loops calculating the factors do the same thing.
The 2 loops printing out the factors do the same thing.

Other issues

Why do you make the arrays 1 larger than the number of factors.
The two print loop prints out the factors from the same array. However if you delete the duplication this will be fixed.
Check the end condition of the print loop. This is why you get the AIOOBE.

4. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## Re: Factor listing issues

When I take away the +1 I get this error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Greatest_Common_Factor.main(Greatest_Common_Factor .java:16)

5. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## Re: Factor listing issues

Ignore the post above this one, it's stupid, I gotta fix everything first

6. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## Re: Factor listing issues

The 2 loops calculating the factors do the same thing. They do the same process but with different variables.
The 2 loops printing out the factors do the same thing. They do the same process but the arrays are different

Other issues

Why do you make the arrays 1 larger than the number of factors. So I don't get this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Greatest_Common_Factor.main(Greatest_Common_Factor .java:16)
Which I get when I delete the + from these
Java Code:
``` factors1 = new int[1 + [getFactor1Length(number)]; //Gets all factors of number
factors2 = new int[1 + [getFactor2Length(number2)]; // Gets all factors of number 2```
The two print loop prints out the factors from the same array. However if you delete the duplication this will be fixed. You're right, fixed it.
Check the end condition of the print loop. This is why you get the AIOOBE. Aha! That's it except it will only print out the numbers if I add the 1 + to the getfactor1length

NEW CODE
Java Code:
```public class Greatest_Common_Factor {
public static void main (String args[]){
int number = 10;
int number2 = 15;
int factor1Length = 0;
int itterations = 0;
int itterations2 = 0;
int[] factors1;
int[] factors2;
factors1 = new int[1+getFactor1Length(number)]; //Gets all factors of number
factors2 = new int[1+getFactor2Length(number2)]; // Gets all factors of number 2

for(int x=1; x<=(number); x++){ //puts all factors of num1 into factors1 array
if(number % x == 0){
itterations++;
factors1[itterations] = x;
}//end if
}//end for

for(int y=1; y<=(number2); y++){
if(number2 % y == 0){
itterations2++;
factors2[itterations2] = y;
}//end if
}//end for

//Print all factors of everything
for(int a=0; a<=factors1.length; a++){
System.out.println("Factor 1 factor " + factors1[a]);
}
for(int b=0; b<=factors2.length; b++){
System.out.println("Factor 2 factor " + factors2[b]);
}

}//end main

//**********************Get Factor 2 Length****************************
private static int getFactor2Length(int number2) {
int factor2Length = 0;
for(int i=1; i<=(number2); i++){
if(number2 % i == 0){
factor2Length++;
}//end if
}//end for
return factor2Length;
}//end method

//**********************Get Factor Length****************************
private static int getFactor1Length(int number) {
int factor1Length = 0;
for(int i=1; i<=(number); i++){
if(number % i == 0){
factor1Length++;
}//end if
}//end for
return factor1Length;
}//end method

}//end class```

ERRORS / RESULT:

Factor 1 factor 0
Factor 1 factor 1
Factor 1 factor 2
Factor 1 factor 5
Factor 1 factor 10
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Greatest_Common_Factor.main(Greatest_Common_Factor .java:29)

7. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## Re: Factor listing issues

WORKING CODE
Java Code:
```
public class Greatest_Common_Factor {
public static void main (String args[]){
int number = 10;
int number2 = 15;
int factor1Length = 0;
int itterations = 0;
int itterations2 = 0;
int[] factors1;
int[] factors2;
factors1 = new int[getFactor1Length(number)]; //Gets all factors of number
factors2 = new int[getFactor2Length(number2)]; // Gets all factors of number 2

for(int x=1; x<=(number); x++){ //puts all factors of num1 into factors1 array
if(number % x == 0){
itterations++;
factors1[itterations-1] = x;
}//end if
}//end for

for(int y=1; y<=(number2); y++){
if(number2 % y == 0){
itterations2++;
factors2[itterations2-1] = y;
}//end if
}//end for

//Print all factors of everything
for(int a=0; a<=factors1.length-1; a++){
System.out.println("Factor 1 factor " + factors1[a]);
}
for(int b=0; b<=factors2.length-1; b++){
System.out.println("Factor 2 factor " + factors2[b]);
}

}//end main

private static int getFactor2Length(int number2) {
int factor2Length = 0;
for(int i=1; i<=(number2); i++){
if(number2 % i == 0){
factor2Length++;
}//end if
}//end for
return factor2Length;
}//end method

//**********************Get Factor Length****************************
private static int getFactor1Length(int number) {
int factor1Length = 0;
for(int i=1; i<=(number); i++){
if(number % i == 0){
factor1Length++;
}//end if
}//end for
return factor1Length;
}//end method

}//end class```
notice the many "-1"
:D

8. ## Re: Factor listing issues

Originally Posted by skaterboy987
notice the many "-1"
:D
That is easily fixed: change this:

Java Code:
```for(int a=0; a<=factors1.length-1; a++){
System.out.println("Factor 1 factor " + factors1[a]);
}```
to this:

Java Code:
```for(int a=0; a< factors1.length; a++){
System.out.println("Factor 1 factor " + factors1[a]);
}```
kind regards,

Jos

9. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## Re: Factor listing issues

Ohhhhhhhhhh! Thats why. Thanks Jos.
Sorry but I have 1 final question. I know there's gotta be some mistake but this is what I'm intending part of the code to do to compare the factors. So lets pretend were comparing 7 to 7 trying to find the the common factors of only 1 and 7. This is what I intend for the program to do.

is 1 = 1
is 1 = 2
is 1 = 3
is 1 = 4
is 1 = 5
is 1 = 6
is 1 = 7
is 2 = 1
is 2 = 2
etc...

Here's the code to compare the numbers
Java Code:
```	//Find Common Factors....
for(int zz=0; zz<(factors1.length); zz++){
for(int z=0; zz<(factors2.length); z++){
if(factors1[zz] == factors2[z]){
System.out.println(factors1[z]);
}//end if
}//end for
}//end for```
It only prints 1 then it prints:
Java Code:
```Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Greatest_Common_Factor.main(Greatest_Common_Factor.java:39)```
Which is line:
Java Code:
`		if(factors1[zz] == factors2[z]){`
It only prints 1
For the error I don't understand whats going on :(
Any help is appreciated!

The full code
Java Code:
```public class Greatest_Common_Factor {
public static void main (String args[]){
int number = 10;
int number2 = 15;
int factor1Length = 0;
int itterations = 0;
int itterations2 = 0;
int[] factors1;
int[] factors2;
factors1 = new int[getFactor1Length(number)]; //Gets all factors of number
factors2 = new int[getFactor2Length(number2)]; // Gets all factors of number 2

for(int x=1; x<=(number); x++){ //puts all factors of num1 into factors1 array
if(number % x == 0){
itterations++;
factors1[itterations-1] = x;
}//end if
}//end for

for(int y=1; y<=(number2); y++){
if(number2 % y == 0){
itterations2++;
factors2[itterations2-1] = y;
}//end if
}//end for

//Print all factors of everything
for(int a=0; a<=factors1.length-1; a++){
System.out.println("Factor 1 factor " + factors1[a]);
}//end for
for(int b=0; b<=factors2.length-1; b++){
System.out.println("Factor 2 factor " + factors2[b]);
}//end for

//Find Common Factors....
for(int zz=0; zz<(factors1.length); zz++){
for(int z=0; zz<(factors2.length); z++){
if(factors1[zz] == factors2[z]){
System.out.println(factors1[z]);
}//end if
}//end for
}//end for
}//end main

private static int getFactor2Length(int number2) {
int factor2Length = 0;
for(int i=1; i<=(number2); i++){
if(number2 % i == 0){
factor2Length++;
}//end if
}//end for
return factor2Length;
}//end method

//**********************Get Factor Length****************************
private static int getFactor1Length(int number) {
int factor1Length = 0;
for(int i=1; i<=(number); i++){
if(number % i == 0){
factor1Length++;
}//end if
}//end for
return factor1Length;
}//end method

}//end class```
Last edited by skaterboy987; 10-28-2011 at 10:51 PM.

10. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## Re: Factor listing issues

anyone know a better way to compare the numbers? One that works?

11. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## Re: Factor listing issues

Someone please respond!

#### Posting Permissions

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