# Thread: Greatest Common Factors (Version 2) Storing and Comparing Factors

1. Member 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: http://www.java-forums.org/new-java/...ng-issues.html

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 is 10. number has 4 factors. We can access the amount of factors number has by using the following method
Java Code:
`getFactorLength(number)`
In this situation though we have 2 numbers. So I get the factors for each number in this for statement.
Java Code:
```for (int a=0; a<(number.length); a++){
System.out.println(getFactorLength(number[a]));//0 and 1
listFactors(number[a]);
}```
Then, the listFactors method prints out the factors of the current number. listFactors uses this code:
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 1-2-3-4-5-6 etc etc
if(number % i == 0){
System.out.println(i);
numberOfFactors++;

}//end if
}//end for
}//end method```
I want to save the factors in the 2d array with this listFactors 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```
With the above method I'm hoping to achieve this if the current number we are using is 10 (number)
10's factors are 1,2,5,10
So I want it to save like this:
factors = 1
factors = 2
factors = 5
factors = 10

However, running the code with the above method brings this output:
4
1
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;
number = 10;
number = 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```
thanks!
Last edited by skaterboy987; 10-30-2011 at 02:22 AM.  Reply With Quote

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

## Re: Greatest Common Factors (Version 2) Storing and Comparing Factors

Anybody understand the problem?  Reply With Quote

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[];
matrix = a1;
matrix = a2;
System.out.println(matrix);
System.out.println(matrix);```  Reply With Quote

4. Member 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.  Reply With Quote

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

## Re: Greatest Common Factors (Version 2) Storing and Comparing Factors Originally Posted by Junky 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[];
matrix = a1;
matrix = a2;
System.out.println(matrix);
System.out.println(matrix);```
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[];
public static void main (String args[]){
boolean hasFoundGreatestCommonFactor = false;
factors = a1;
factors = a2;

int number[];
number = new int;
number = 10;
number = 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```
it errors :/
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)  Reply With Quote

6. ## Re: Greatest Common Factors (Version 2) Storing and Comparing Factors

Ooops!

Now I see it.  Reply With Quote

7. Member 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?  Reply With Quote

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.  Reply With Quote

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.  Reply With Quote

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

## Re: Greatest Common Factors (Version 2) Storing and Comparing Factors Originally Posted by Junky 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.
But doesn't that mean the array then cannot be public?  Reply With Quote

11. Member 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.  Reply With Quote

12. Member 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;
number = 100;
number = 10000;
number = 20;
number = 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])
{
}
}
}

//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){
}//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```  Reply With Quote

2d array, factors, math, methods, static array 