Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: binarySearch troubles

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default binarySearch troubles

    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


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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.
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: binarySearch troubles

    Hey,
    I made a new method for it:
    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

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: binarySearch troubles

    What is the actual problem? Where is the problem?
    You must provide SSCE
    Last edited by Mr.777; November 3rd, 2011 at 07:45 AM.

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

  6. #6
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: binarySearch troubles

    Did you try debugging this part of your code?

  8. #8
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: binarySearch troubles

    No, how do i do that? (I use BlueJ, btw)

  9. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

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

  10. #10
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: binarySearch troubles

    ThankYou, I figured out what was wrong with it.

Similar Threads

  1. InnerClass troubles
    By pottsiex5 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 18th, 2011, 02:25 PM
  2. .setVisible() troubles
    By pottsiex5 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 5th, 2011, 03:28 AM
  3. array troubles
    By mike2452 in forum Collections and Generics
    Replies: 3
    Last Post: August 14th, 2011, 07:22 PM
  4. JSP Troubles
    By sdkeslar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 12th, 2010, 02:26 PM
  5. Array Troubles
    By Leeds_Champion in forum Collections and Generics
    Replies: 6
    Last Post: October 22nd, 2009, 11:05 AM