Results 1 to 11 of 11
Thread: Factor listing issues
 10282011, 06:08 AM #1Member
 Join Date
 Sep 2011
 Posts
 56
 Rep Power
 0
Factor comparing issues
OUTDATED, PLEASE DO NOT RESPOND
new post: Greatest Common Factors (Version 2) Storing and Comparing Factors
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; 10302011 at 03:17 AM.
 10282011, 06:58 AM #2
Re: Factor listing issues
Why do you have 2 methods (getFactorLength) that do the same thing?
 10282011, 07:14 AM #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.
 10282011, 09:00 PM #4Member
 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)
 10282011, 09:02 PM #5Member
 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
 10282011, 09:14 PM #6Member
 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
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)
 10282011, 10:20 PM #7Member
 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[itterations1] = x; }//end if }//end for for(int y=1; y<=(number2); y++){ if(number2 % y == 0){ itterations2++; factors2[itterations21] = y; }//end if }//end for //Print all factors of everything for(int a=0; a<=factors1.length1; a++){ System.out.println("Factor 1 factor " + factors1[a]); } for(int b=0; b<=factors2.length1; 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
:D
 10282011, 10:25 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,945
 Blog Entries
 7
 Rep Power
 22
Re: Factor listing issues
That is easily fixed: change this:
Java Code:for(int a=0; a<=factors1.length1; a++){ System.out.println("Factor 1 factor " + factors1[a]); }
Java Code:for(int a=0; a< factors1.length; a++){ System.out.println("Factor 1 factor " + factors1[a]); }
JosI have the stamina of a seal; I lie on the beach instead of running on it.
 10282011, 10:46 PM #9Member
 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
Java Code:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at Greatest_Common_Factor.main(Greatest_Common_Factor.java:39)
Java Code:if(factors1[zz] == factors2[z]){
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[itterations1] = x; }//end if }//end for for(int y=1; y<=(number2); y++){ if(number2 % y == 0){ itterations2++; factors2[itterations21] = y; }//end if }//end for //Print all factors of everything for(int a=0; a<=factors1.length1; a++){ System.out.println("Factor 1 factor " + factors1[a]); }//end for for(int b=0; b<=factors2.length1; 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; 10282011 at 10:51 PM.
 10292011, 10:51 PM #10Member
 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?
 10302011, 12:38 AM #11Member
 Join Date
 Sep 2011
 Posts
 56
 Rep Power
 0
Similar Threads

need help with factor quadratic...
By frostkarrotor in forum New To JavaReplies: 3Last Post: 11302010, 01:28 AM 
Finding Largest Prime Factor
By perito in forum New To JavaReplies: 7Last Post: 11082010, 09:25 PM 
greatest prime factor
By java_prgr in forum New To JavaReplies: 2Last Post: 07232010, 08:28 PM 
Listing Installed DSA
By anoopasta in forum XMLReplies: 1Last Post: 05062010, 04:10 PM 
Returning the Greatest Prime Factor
By BJ1110 in forum New To JavaReplies: 15Last Post: 10232009, 10:06 PM
Bookmarks