Results 1 to 12 of 12
 10302011, 03:16 AM #1Member
 Join Date
 Sep 2011
 Posts
 56
 Rep Power
 0
Greatest Common Factors (Version 2) Storing and Comparing Factors
I'm just going to start a new thread for the same project, as I've changed the code a a lot.
Old post: Factor listing issues
The part of the code I'm working on right now is supposed to save the factors in a 2d array. I've got little to no experience using a 2d array. Here's how I would like it to work. So number[0] is 10. number[0] has 4 factors. We can access the amount of factors number[0] has by using the following method
Java Code:getFactorLength(number[0])
Java Code:for (int a=0; a<(number.length); a++){ System.out.println(getFactorLength(number[a]));//0 and 1 listFactors(number[a]); }
Java Code://*****************************List Factors******************* private static void listFactors(int number) { int numberOfFactors = 1; for(int i=1; i<=(number); i++){ //Counting to the current number, ie 123456 etc etc if(number % i == 0){ System.out.println(i); numberOfFactors++; }//end if }//end for }//end method
Java Code://*****************************List Factors******************* private static void listFactors(int number) { int numberOfFactors = 1; for(int i=1; i<=(number); i++){ //Counting to 10 if(number % i == 0){ System.out.println(i); numberOfFactors++; factors[number][numberOfFactors] = i; //< Does not work }//end if }//end for }//end method
10's factors are 1,2,5,10
So I want it to save like this:
factors[0][1] = 1
factors[0][2] = 2
factors[0][3] = 5
factors[0][4] = 10
However, running the code with the above method brings this output:
4
1
Exception in thread "main" java.lang.NullPointerException
at Greatest_Common_Factor.listFactors(Greatest_Common _Factor.java:32)
at Greatest_Common_Factor.main(Greatest_Common_Factor .java:16)
Remember, this code is a debug version so it prints out like this when you're not saving it to an array(printing it directly from listfactors() and (getFactorLength())
4 < Number of factors of 10
1 <First Factor of 10
2 <Second Factor of 10
5 <Third Factor of 10
10 <Fourth Factor of 10
4 <Number of factors in 15
1 <First Factor of 15
3 <Second Factor of 15
5 <Third Factor of 15
15 <Final Factor of 15
So any help with the 2d arrays would be greatly appreciated. Here is my full code:
Java Code:public class Greatest_Common_Factor { static int factors[][]; public static void main (String args[]){ boolean hasFoundGreatestCommonFactor = false; int number[]; number = new int[2]; number[0] = 10; number[1] = 15; //while(hasFoundGreatestCommonFactor == false){ for (int a=0; a<(number.length); a++){ System.out.println(getFactorLength(number[a]));//0 and 1 listFactors(number[a]); }//end for // }//end while }//end main //*****************************List Factors******************* private static void listFactors(int number) { int numberOfFactors = 1; for(int i=1; i<=(number); i++){ //Counting to 10 if(number % i == 0){ System.out.println(i); numberOfFactors++; // factors[number][numberOfFactors] = i; }//end if }//end for }//end method //**********************Get Factor Length**************************** private static int getFactorLength(int currentNumber) { int factor1Length = 0; for(int i=1; i<=(currentNumber); i++){ if(currentNumber % i == 0){ factor1Length++; }//end if }//end for return factor1Length; }//end method }//end class
Last edited by skaterboy987; 10302011 at 03:22 AM.
 10312011, 01:28 AM #2Member
 Join Date
 Sep 2011
 Posts
 56
 Rep Power
 0
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
Anybody understand the problem?
 10312011, 02:31 AM #3
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
A 2D array is just an array of arrays. Which means that it is a 1D array and each element is another 1D array. Look at the following code:
Java Code:int[] a1 = {1,2,3}; int[] a2 = {9,8,7,6,5,4}; int[][] matrix = new int[2][]; matrix[0] = a1; matrix[1] = a2; System.out.println(matrix[0][0]); System.out.println(matrix[1][3]);
 10312011, 02:34 AM #4Member
 Join Date
 Oct 2011
 Posts
 92
 Rep Power
 0
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
So, if i've got this right, the issue here is that you can't assign the variables too a two dimensional Array and you're getting a null pointer exception. That usually means an object hasn't been initialised properly. I don't like giving the answer, removes the whole fun of problem solving. Just look at your fields, and keep in mind that something might not have been initialized properly.
 10312011, 02:39 AM #5Member
 Join Date
 Sep 2011
 Posts
 56
 Rep Power
 0
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
When I do that, with this new code
Java Code:public class Greatest_Common_Factor { static int a1[]; static int a2[]; static int[][] factors = new int[2][]; public static void main (String args[]){ boolean hasFoundGreatestCommonFactor = false; factors[0] = a1; factors[1] = a2; int number[]; number = new int[2]; number[0] = 10; number[1] = 15; //while(hasFoundGreatestCommonFactor == false){ for (int a=0; a<(number.length); a++){ System.out.println(getFactorLength(number[a]));//0 and 1 listFactors(number[a]); }//end for // }//end while }//end main //*****************************List Factors******************* private static void listFactors(int number) { int numberOfFactors = 1; for(int i=1; i<=(number); i++){ //Counting to 10 if(number % i == 0){ System.out.println(i); numberOfFactors++; factors[number][numberOfFactors] = i; }//end if }//end for }//end method //**********************Get Factor Length**************************** private static int getFactorLength(int currentNumber) { int factor1Length = 0; for(int i=1; i<=(currentNumber); i++){ if(currentNumber % i == 0){ factor1Length++; }//end if }//end for return factor1Length; }//end method }//end class
4
1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Greatest_Common_Factor.listFactors(Greatest_Common _Factor.java:35)
at Greatest_Common_Factor.main(Greatest_Common_Factor .java:19)
 10312011, 02:42 AM #6
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
Ooops!
Now I see it.
 10312011, 02:43 AM #7Member
 Join Date
 Oct 2011
 Posts
 92
 Rep Power
 0
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
Out of curiosity, why is it 2 and 0?
 10312011, 02:44 AM #8
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
OK what I was suggesting is to use the listFactors method to create and return an array of all the factors of the int passed as a parameter. Then back in the main method assign that returned array to the appropriate element of your 2D array.
 10312011, 02:46 AM #9
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
BTW the code I posted in reply 3 was just an example. You were supposed to adapt it to your needs not just paste in directly into your code.
 10312011, 02:46 AM #10Member
 Join Date
 Sep 2011
 Posts
 56
 Rep Power
 0
 10312011, 02:48 AM #11Member
 Join Date
 Oct 2011
 Posts
 92
 Rep Power
 0
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
You can declare the array publicly, and initialize it inside the listFactors() method.
 11012011, 03:47 AM #12Member
 Join Date
 Sep 2011
 Posts
 56
 Rep Power
 0
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
Hur Dur..
Java Code:import java.util.ArrayList; import java.util.List; public class Greatest_Common_Factor { static int factors[][]; public static void main (String args[]){ boolean hasFoundGreatestCommonFactor = false; int[] number = new int[4]; number[0] = 100; number[1] = 10000; number[2] = 20; number[3] = 250; Factors[] theFactors = new Factors[number.length]; for(int i = 0; i < number.length; i++) { theFactors[i] = new Factors(); theFactors[i].Number = number[i]; theFactors[i].Factors = listFactors(number[i]); }//end for //loop through all the objects for(int i = 0; i < theFactors.length; i++) { System.out.print("Factors for " + theFactors[i].Number + ": "); //loop through the factors within this object for(int j = 0; j < theFactors[i].Factors.length; j++) { System.out.print(theFactors[i].Factors[j] + " "); }//end for System.out.println(); } //loop through all of them to find the common factors Integer[] commonFactors = null; for(int i = 0; i < theFactors.length; i++) { if(commonFactors == null) { commonFactors = theFactors[i].Factors; continue; } commonFactors = getCommonFactors(theFactors[i].Factors, commonFactors); } //we now have an array of common factor, lets print it out! System.out.print("The common factors are:"); int biggestCommonFactor = 1; for(int i = 0; i < commonFactors.length; i++) { if(commonFactors[i] > biggestCommonFactor) { biggestCommonFactor = commonFactors[i]; } System.out.print(commonFactors[i]); if(i+1 != commonFactors.length) { System.out.print(", "); } } System.out.println(); System.out.println("The largest common factor is: " + biggestCommonFactor); }//end main private static Integer[] getCommonFactors(Integer[] arrayA, Integer[] arrayB) { //we'll put these into an array list, because we don't know how many common factors we'll have ArrayList<Integer> commonFactorList = new ArrayList<Integer>(); //now we use a nested loop to find the common factors //loop through the current common factors for(int i = 0; i < arrayA.length; i++) { //now loop through the candiate factors for(int j = 0; j < arrayB.length; j++) { if(arrayB[j] == arrayA[i]) { commonFactorList.add(arrayA[i]); } } } //this takes the array list (which is expandable) and turns it into a simple array return (Integer[])commonFactorList.toArray(new Integer[commonFactorList.size()]); } //****************************List Factors******************************* private static Integer[] listFactors(int number) { //array lists grow, we use them when we don't know how long it will be ArrayList<Integer> factors = new ArrayList<Integer>(); for(int i=1; i<=(number); i++){ //Counting to any number if(number % i == 0){ factors.add(i); }//end if }//end for //this takes the array list (which is expandable) and turns it into a simple array return (Integer[])factors.toArray(new Integer[factors.size()]); }//end method //*********************Get Factor Length******************************* private static int getFactorLength(int currentNumber) { int factor1Length = 0; for(int i=1; i<=(currentNumber); i++){ if(currentNumber % i == 0){ factor1Length++; }//end if }//end for return factor1Length; }//end method }//end class
Similar Threads

Stack Of Integers (to get factors)
By aramiky818 in forum New To JavaReplies: 2Last Post: 05012011, 01:49 AM 
Finding Prime Factors
By Need Help in forum New To JavaReplies: 3Last Post: 04092011, 01:14 PM 
greatest prime factor
By java_prgr in forum New To JavaReplies: 2Last Post: 07232010, 08:28 PM 
java version pointing to older version
By deepakts in forum New To JavaReplies: 4Last Post: 05062010, 09:59 AM 
Returning the Greatest Prime Factor
By BJ1110 in forum New To JavaReplies: 15Last Post: 10232009, 10:06 PM
Bookmarks