/*
 * Search.java by Richard J. Davies
 * from `Introductory Java for Scientists and Engineers'
 * chapter: `Programming Practices'
 * section: `Example - Binary Search'
 *
 * This program performs a binary search on an array.
 */
import java.io.*;

public class Search
{
  // The array to be searched.
  public static final int[] data
             = { 2, 7, 17, 32,
                 45, 76, 98, 101,
                 121, 143, 155, 187,
                 201, 257, 312, 345 };

  // A means of getting user input
  // see Section 5.8 for
  // details. JDK 1.1 or later.
  public static final BufferedReader b
    = new BufferedReader(
        new InputStreamReader(System.in));


  // The `main' method actually implements
  // binary search.
  public static void main(String[] argv)
                throws IOException
  {
    // The start and end of the area remaining
    // to be searched. The start point is
    // included, the end point is not.
    int startrem = 0;
    int endrem = 16;

    // Ask the user for a value to find
    System.out.println("Input a value to find");
    String s = b.readLine();

    int val = Integer.parseInt(s);

    // Perform binary search
    while (endrem > startrem + 1)
    {
      int mid = (startrem + endrem)/2;

      if (data[mid] > val)
        endrem = mid;
      else
        startrem = mid;
    }

    if (data[startrem] == val)
      System.out.println("Data was found");
    else
      System.out.println("Data was not found");
  }
}

