Results 1 to 4 of 4
  1. #1
    piyowashere is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default index out of bounds error combining numbers help

    Hello guys i am trying to get a program that writes down all posible combinations of a number. For example if i put 123, it would give me 123,132,213,231,312,321
    i wrote a code that works up until 123456789. When i try to put 1234567891, it gives me an an error: (or any number that is over 9 digits)
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 40320, Size: 40320

    here is the code:

    Java Code:
    public  ArrayList combinar(int numeros){
    		
    		
    		ArrayList<String> combinaciones = new ArrayList();
    		String numerosString = "" + numeros;
    		
    		numerosString = numerosString.replace(".0", "");
    		
    		if(numerosString.length()<=2){
    			combinaciones.add(numerosString);
    			char primerDigito = numerosString.charAt(0);
    			char segundoDigito = numerosString.charAt(1);
    			String segundaCombinacion = "" + segundoDigito + primerDigito;
    			combinaciones.add(segundaCombinacion);
    			return combinaciones;
    		}
    		
    		
    		int nCombinaciones = factorial(numerosString.length());
    		int numeroVecesRepetirPrimerNumero = nCombinaciones/numerosString.length();
    		
    		for(int i = 0;i<numerosString.length();i++){
    			
    			char primerDigito = numerosString.charAt(i);
    			String resto = removeChar(numerosString, i);
    			double restoNumeros = Double.parseDouble(resto);
    			ArrayList combinacionesDeResto = combinar((int) restoNumeros);
    			
    			for(double j = 0;j<numeroVecesRepetirPrimerNumero;j++){
    				
    				combinaciones.add("" + primerDigito + combinacionesDeResto.get((int) j));
    			}
    		}
    	
    		return combinaciones;	
    		
    		
    	}
    	
    	public int factorial(int numero){
    		if(numero<=1){
    			return numero;
    		}
    		return numero*factorial(numero-1);
    	}
    this is how i use the method:

    Java Code:
    ArrayList list = combinador.combinar(1234567891);
    combinador.writeToFile("C:/Users/Jose/Desktop/sudokus",list);
    this is the error i get:
    Java Code:
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 40320, Size: 40320
    	at java.util.ArrayList.rangeCheck(Unknown Source)
    	at java.util.ArrayList.get(Unknown Source)
    	at Testing.CombinadorDeNumeros.combinar(CombinadorDeNumeros.java:42)
    	at Testing.CombinadorDeNumeros.main(CombinadorDeNumeros.java:101)

    this is line 42:

    Java Code:
    combinaciones.add("" + primerDigito + combinacionesDeResto.get((int) j));
    Thanks!!
    Last edited by piyowashere; 05-25-2012 at 08:57 PM.

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

    Default Re: index out of bounds error combining numbers help

    Be careful with those factorials; an int can only hold a number as large as pow(2, 31)-1 (~ two billion)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    piyowashere is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: index out of bounds error combining numbers help

    Hey thanks for replying!

    i made the factorial return a double but I still get the same error :(

    help please

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

    Default Re: index out of bounds error combining numbers help

    To tell you the truth: I don't understand your algorithm; care to explain it first?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. array index out of bounds
    By searcher in forum New To Java
    Replies: 3
    Last Post: 01-21-2012, 03:27 PM
  2. Replies: 4
    Last Post: 01-31-2011, 09:20 PM
  3. Array index out of bounds error
    By blackstyle18 in forum New To Java
    Replies: 3
    Last Post: 12-28-2010, 03:37 AM
  4. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 08:42 AM
  5. Array Index out of bounds
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 06:11 AM

Posting Permissions

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