4

I am trying to find the index of a string in a string array using the Arrays.binarySearch() method however the method seems to be return the postion integer "-5" when looking for the string "Free". Any idea why this would be?

String[] names = {"Arken","Ben","Darklark", "Free","group"};

void changeFriends(String uname, boolean b)
        {   // change a friend's "online" status

    Arrays.sort(names);
    int index = Arrays.binarySearch(names, uname);

    System.out.println("NAME OF ONLINE USER IS AT INDEX:" + index + "Name:" + uname);
        if(index > -1)
        {

             if(b == true)
            {
                loggedOn[index] = true;
            }
            else
            {
                loggedOn[index] = false;
            }
        }
        // call method to update buttons
        changeNameButtons();
    }
1
  • 1
    I tested your code, it returns NAME OF ONLINE USER IS AT INDEX: 3 Name:Free Commented Dec 5, 2012 at 23:56

4 Answers 4

14

If it's returning a negative value it's not found:

http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html

public static int binarySearch(Object[] a,
                               Object key)

Returns: index of the search key, if it is contained in the array; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

Whatever you're passing in as uname is not "Free". I highly suspect you're thinking case doesn't matter (or have trailing characters; whitespace or newline) ;)

Sign up to request clarification or add additional context in comments.

Comments

9

Those who are still having this trouble like I was having earlier, and you are thinking that why can't anyone else see that it(Arrays.binarySearch) still have some problems because you have inserted the correct values and you still get the negative results. Well this answer is for those:

It is because your array is not sorted, my friend. Even if it's of characters or strings

For arrays.binarySearch the array should be sorted(I know, you must be thinking that How did I miss the most important thing (Well yeah, that happens)).

1 Comment

Incredible but true!
6

Hmm, i just ran your code and i got the index 3.

Arrays.sort(names);
int index = Arrays.binarySearch(names, "Free");
System.out.println(index);

you probably are searching free or Free(with a trailing white space) instead of Free, in which case it returns -5.

6 Comments

@MadProgrammer haha, i was not being rude, anyhow chhanged it :)
I didn't think you where, but not everyone will see it that way ;)
@MadProgrammer I see no 'harsh' comment in any of the edits. What are you talking about?
@EJP - It was simply my opinion, it just felt a "little" off hand. Remember, the meaning of communication is the meaning that the receiver makes of it ;)
I have the Arrays.binarySearch returning -10 when searching for the underscore character _ Doing it in debbuger plain sight.
|
1

If value is not found and value is less than one or more elements in array , the negative number returned is the bitwise complement of the index of the first element that is larger than value

1 Comment

You should post your code and problems not just post the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.