- Joined
- Aug 12, 2002
- Location
- Howell, Michigan
I can get it to compile, but it never gets to the part where it finds it or doesn't.. Goes through the while loop 4 times, no matter what the entry is.
public class BinarySearch
{
public static void main(String[] args) throws Exception
{
char[] orderedList = {'A','B','E','G','L','P','V','W','Y','Z'};
System.out.println("Enter a capital letter to search for: ");
int x = System.in.read();
int count = 0;
int low = 0;
int high = orderedList.length - 1;
while (low <= high) {
count++;
int mid = (low + high) / 2;
if (x < mid)
high = mid - 1;
else if (x > mid)
low = mid + 1;
else
System.out.println("It has been found!");
}
if (high > low)
System.out.println("Number wasn't found");
System.out.println(count);
}
}
public class BinarySearch
{
public static void main(String[] args) throws Exception
{
char[] orderedList = {'A','B','E','G','L','P','V','W','Y','Z'};
System.out.println("Enter a capital letter to search for: ");
int x = System.in.read();
int count = 0;
int low = 0;
int high = orderedList.length - 1;
while (low <= high) {
count++;
int mid = (low + high) / 2;
if (x < mid)
high = mid - 1;
else if (x > mid)
low = mid + 1;
else
System.out.println("It has been found!");
}
if (high > low)
System.out.println("Number wasn't found");
System.out.println(count);
}
}