Results 1 to 11 of 11
Thread: Factor listing issues
- 10-28-2011, 06:08 AM #1
Member
- 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 classLast edited by skaterboy987; 10-30-2011 at 03:17 AM.
- 10-28-2011, 06:58 AM #2
Re: Factor listing issues
Why do you have 2 methods (getFactorLength) that do the same thing?
- 10-28-2011, 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.
- 10-28-2011, 09:00 PM #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)
- 10-28-2011, 09:02 PM #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
- 10-28-2011, 09:14 PM #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
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.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)
- 10-28-2011, 10:20 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 56
- Rep Power
- 0
Re: Factor listing issues
WORKING CODE
notice the many "-1"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
:D
- 10-28-2011, 10:25 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Factor listing issues
That is easily fixed: change this:
to this:Java Code:for(int a=0; a<=factors1.length-1; a++){ System.out.println("Factor 1 factor " + factors1[a]); }
kind regards,Java Code:for(int a=0; a< factors1.length; a++){ System.out.println("Factor 1 factor " + factors1[a]); }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-28-2011, 10:46 PM #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
It only prints 1 then it prints: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
Which is line:Java Code:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at Greatest_Common_Factor.main(Greatest_Common_Factor.java:39)
It only prints 1Java 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[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 classLast edited by skaterboy987; 10-28-2011 at 10:51 PM.
- 10-29-2011, 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?
- 10-30-2011, 12:38 AM #11
Member
- 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: 11-30-2010, 12:28 AM -
Finding Largest Prime Factor
By perito in forum New To JavaReplies: 7Last Post: 11-08-2010, 08:25 PM -
greatest prime factor
By java_prgr in forum New To JavaReplies: 2Last Post: 07-23-2010, 08:28 PM -
Listing Installed DSA
By anoopasta in forum XMLReplies: 1Last Post: 05-06-2010, 04:10 PM -
Returning the Greatest Prime Factor
By BJ1110 in forum New To JavaReplies: 15Last Post: 10-23-2009, 10:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks