Can someone pls give me a fragment code on how to do a binary search using java....

I want to search it by name

Say for instance,

System.out.print("Enter name you wish to search:");

//My array of name is already sorted...
My only concern is how to do a binary search...

pls.... help me.....It is my project in school...

If your array is an array of Strings the you can use the compareTo method:

String s1 = "a";
String s2 = "b";
int i = s1.compareTo(s2);

This method's return values represents which String is greater or lower than the other. So you can use that method for the comparison.

As for the binary search, write a loop that does the following:

>Check if the value to search is in the middle of the array.
>if it's not, check whether it is in the first half or the second half of the array
>After you have found at which half it is, repeat the search, but this time you will not check the entire array but only the half that you found where the value to search is.

remember that the array is already sorted, so the "lower" strings will be at the beginning. That is how you will find which half to choose.