www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Word list combinations

  1. #1
    Join Date
    May 2007
    Location
    Sweden
    Posts
    123

    resolved [RESOLVED] Word list combinations

    Hi

    I'm making a program that will count how many different combinations you can get of a string. The counting works fine, but when I tried to make a function that creates a list of words with combinations, I get java.lang.ArrayIndexOutOfBoundsException.
    The function is attached below and is used this way:
    Code:
    int c = calculateCombinations(t.getText());
    String[] arr = new String[c];
    getWordList(createWordList(arr, "", t.getText()))
    I've tried to put other numbers than c, but it doesn't matter. The function should work but I think there is a stack problem in the createWordList().
    Can somebody help me with this?
    Attached Files Attached Files

  2. #2
    Join Date
    Aug 2008
    Posts
    1
    Ever heard of debugging? You can actually see where it goes wrong...
    Anyway, the reason why you have this OutOfBoundException is because you are trying to set a element of the array at index = length.
    Index is 0-based, length = 1 based (number of elements in the array).

    Eg.:
    Given: An array "test" with 4 element
    the result of test.length is "4". The index of 4th element is '3'. That's why you get this exception.

    *Edit* this happens in your text-file at line 33...

  3. #3
    Join Date
    May 2007
    Location
    Sweden
    Posts
    123
    Sorry, but I don't understand. What am I suppost to do?

    Edit: Added a extern int to the function, works fine...
    Code:
    int arrayIndex = 0;
    private String[] createWordList(String[] wordArray, String strbase, String remainingchars){
    	if(remainingchars.length()==1){
    		wordArray[arrayIndex] = strbase+remainingchars.charAt(0);
    		arrayIndex++;
    	}else{
    		for(int j=0; j<remainingchars.length(); j++) {
    			char currchar = remainingchars.charAt(j);
    			if(remainingchars.indexOf(currchar)==j)
    				wordArray = createWordList(wordArray, strbase+remainingchars.charAt(j), remainingchars.substring(0,j)+remainingchars.substring(j+1,remainingchars.length()));
    		 }
    	}
    	return wordArray;
    }
    Last edited by Totte_ch; 08-04-2008 at 09:13 AM. Reason: sloved the problem

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles