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

    Default 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 class
    Last edited by skaterboy987; 10-30-2011 at 03:17 AM.

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

    Default Re: Factor listing issues

    Why do you have 2 methods (getFactorLength) that do the same thing?

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

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

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

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

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

    Default Re: Factor listing issues

    Ignore the post above this one, it's stupid, I gotta fix everything first

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

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

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

    Default Re: Factor listing issues

    WORKING 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]);
    	}
    	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
    notice the many "-1"
    :D

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,661
    Blog Entries
    7
    Rep Power
    21

    Default Re: Factor listing issues

    Quote Originally Posted by skaterboy987 View Post
    notice the many "-1"
    :D
    That is easily fixed: change this:

    Java Code:
    for(int a=0; a<=factors1.length-1; a++){
             System.out.println("Factor 1 factor " + factors1[a]);     
    }
    to this:

    Java Code:
    for(int a=0; a< factors1.length; a++){
             System.out.println("Factor 1 factor " + factors1[a]);     
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default 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
    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
    It only prints 1 then it prints:
    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    	at Greatest_Common_Factor.main(Greatest_Common_Factor.java:39)
    Which is line:
    Java Code:
    		if(factors1[zz] == factors2[z]){
    It only prints 1
    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 class
    Last edited by skaterboy987; 10-28-2011 at 10:51 PM.

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

    Default Re: Factor listing issues

    anyone know a better way to compare the numbers? One that works?

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

Similar Threads

  1. need help with factor quadratic...
    By frostkarrotor in forum New To Java
    Replies: 3
    Last Post: 11-30-2010, 12:28 AM
  2. Finding Largest Prime Factor
    By perito in forum New To Java
    Replies: 7
    Last Post: 11-08-2010, 08:25 PM
  3. greatest prime factor
    By java_prgr in forum New To Java
    Replies: 2
    Last Post: 07-23-2010, 08:28 PM
  4. Listing Installed DSA
    By anoopasta in forum XML
    Replies: 1
    Last Post: 05-06-2010, 04:10 PM
  5. Returning the Greatest Prime Factor
    By BJ1110 in forum New To Java
    Replies: 15
    Last Post: 10-23-2009, 10:06 PM

Posting Permissions

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