Factor listing issues

• 10-28-2011, 06:08 AM
skaterboy987
Factor comparing issues
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)

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```
• 10-28-2011, 06:58 AM
Junky
Re: Factor listing issues
Why do you have 2 methods (getFactorLength) that do the same thing?
• 10-28-2011, 07:14 AM
Junky
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.
• 10-28-2011, 09:00 PM
skaterboy987
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)
• 10-28-2011, 09:02 PM
skaterboy987
Re: Factor listing issues
Ignore the post above this one, it's stupid, I gotta fix everything first
• 10-28-2011, 09:14 PM
skaterboy987
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
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
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)
• 10-28-2011, 10:20 PM
skaterboy987
Re: Factor listing issues
WORKING CODE
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
• 10-28-2011, 10:25 PM
JosAH
Re: Factor listing issues
Quote:

Originally Posted by skaterboy987
notice the many "-1"
:D

That is easily fixed: change this:

Code:

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

Code:

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

Jos
• 10-28-2011, 10:46 PM
skaterboy987
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
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:
Code:

```Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4         at Greatest_Common_Factor.main(Greatest_Common_Factor.java:39)```
Which is line:
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
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```
• 10-29-2011, 10:51 PM
skaterboy987
Re: Factor listing issues
anyone know a better way to compare the numbers? One that works?
• 10-30-2011, 12:38 AM
skaterboy987
Re: Factor listing issues