Re: binarySearch troubles
The only methods an array has is the ones it inherits from Object. Looking! Looking! Nope. Object does not have a binarySearch method. You can either write the method yourself or you can take a look at the Arrays class.
Re: binarySearch troubles
Hey,
I made a new method for it:
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 = checkArray(array, myString.toString(), permu);
if (search>0){
array[arrayNum]= myString.toString();
arrayNum++;
}
}
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
System.out.println(array[3]);
System.out.println(array[4]);
System.out.println(array[5]);
}
public static int checkArray(String[] array, String checkee, int permu){
int counter = 0;
int returner=0;
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
System.out.println(array[3]);
System.out.println(array[4]);
System.out.println(array[5]);
while (counter<permu){
if (checkee != array[counter]){
returner++;
}
counter++;
}
System.out.println("Returner:"+returner);
return returner;
}
}
However, each time it adds to counter, even if it is equal to the value. How can i fix this?
Joel
Re: binarySearch troubles
What is the actual problem? Where is the problem?
You must provide SSCE
Re: binarySearch troubles
So, the method checks to see if the string matches with a slot in the array, or array[counter]. If the string and the array slot are not equal, it adds 1 to the value that gets returned. If the value that gets returned is greater than 0, then it adds the new permutation to the array... i realize this is kindof a spaghetti code...
Re: binarySearch troubles
My problem is, that in the method that checks if the string matches the array slot, it adds 1 to the return value, even if it is equal to it... So it will always add it to the array even though its already in it...
Code :
public static int checkArray(String[] array, String checkee, int permu){
int counter = 0;
int returner=0;
while (counter<permu){
if (checkee != array[counter]){
returner++;
}
counter++;
}
System.out.println("Returner:"+returner);
return returner;
}
}
Re: binarySearch troubles
Did you try debugging this part of your code?
Re: binarySearch troubles
No, how do i do that? (I use BlueJ, btw)
Re: binarySearch troubles
Well, you read your IDE documentation or if you can't find, try using System.out.println() after consecutive statements and try to find where actually the problem is.
Re: binarySearch troubles
ThankYou, I figured out what was wrong with it.