Hi,
Im making a program that scrambles a word into all the possible permutations. So i swap letters in the string randomly to make a new string. I then want to search the array to see if that string allready exist in the array, however when i use binarySearch it says it cannot find the method. Here is my code:
import java.util.Arrays; import java.util.Random; import java.lang.StringBuilder; public class StringScramble { public static void main (String[] args) { StringBuffer myString = new StringBuffer ("abc"); int length = myString.length(); int permu = 1; Random randomGenerator = new Random(); while (length > 1){ permu *= length; length--; System.out.println(permu); } int arrayNum = 0; String[] array = new String[permu]; array[arrayNum] = myString.toString(); arrayNum++; while(arrayNum < permu){ //initialize variables int length2 = myString.length(); int randomInt1 = randomGenerator.nextInt(length2); char swap1 = myString.charAt(randomInt1); int randomInt2 = randomGenerator.nextInt(length2); char swap2 = myString.charAt(randomInt2); char temp; //the switch temp = swap1; swap1=swap2; swap2=temp; //set it in the string myString.setCharAt(randomInt1, swap1); myString.setCharAt(randomInt2, swap2); int search = array.binarySearch(array, myString.toString()); if (!(search>=0)){ array[arrayNum]= myString.toString(); arrayNum++; } } } }
Also, if someone knows of a easier way to find permutations, that would be helpful too
Thanks
Joel