Results 1 to 12 of 12
  1. #1
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default 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])
    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[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:
    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
    thanks!
    Last edited by skaterboy987; 10-30-2011 at 03:22 AM.

  2. #2
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

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

    Anybody understand the problem?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default 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]);

  4. #4
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default 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.

  5. #5
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

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

    Quote Originally Posted by Junky View Post
    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]);
    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
    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)

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

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

    Ooops!

    Now I see it.

  7. #7
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

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

    Out of curiosity, why is it 2 and 0?

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default 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.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default 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.

  10. #10
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

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

    Quote Originally Posted by Junky View Post
    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?

  11. #11
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

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

    You can declare the array publicly, and initialize it inside the listFactors() method.

  12. #12
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default 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

  1. Stack Of Integers (to get factors)
    By aramiky818 in forum New To Java
    Replies: 2
    Last Post: 05-01-2011, 01:49 AM
  2. Finding Prime Factors
    By Need Help in forum New To Java
    Replies: 3
    Last Post: 04-09-2011, 01:14 PM
  3. greatest prime factor
    By java_prgr in forum New To Java
    Replies: 2
    Last Post: 07-23-2010, 08:28 PM
  4. java -version pointing to older version
    By deepakts in forum New To Java
    Replies: 4
    Last Post: 05-06-2010, 09:59 AM
  5. Returning the Greatest Prime Factor
    By BJ1110 in forum New To Java
    Replies: 15
    Last Post: 10-23-2009, 10:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •