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[0] is 10. number[0] has 4 factors. We can access the amount of factors number[0] has by using the following method
Code:
getFactorLength(number[0])
In this situation though we have 2 numbers. So I get the factors for each number in this for statement.
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:
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
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[0])
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:
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
thanks!
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
Anybody understand the problem?
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:
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]);
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.
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
Quote:
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:
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]);
When I do that, with this new code
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
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)
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
Out of curiosity, why is it 2 and 0?
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.
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.
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
Quote:
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?
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
You can declare the array publicly, and initialize it inside the listFactors() method.
Re: Greatest Common Factors (Version 2) Storing and Comparing Factors
Hur Dur..
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