/** This program is intended to be used with the book "Java 7 -- What Do You Want To Do?"
 * by Steven P. Warr and is distributed free of charge and without and expectations of remuneration.
 * Chapter ALG   NOTE most program names begin with the title of the chapter in which
 * they are intended to be used.   Copyright 2010 Steven P. Warr All rights reserved
 */

import javax.swing.JOptionPane; 
import java.util.*; 
import java.io.*; 
public class Alg04 
{
    public static void main(String[ ] args) throws IOException
    {
    	Scanner infile = new Scanner(new File("alg04.data"));
		Scanner keyIn = new Scanner(System.in);
		int [] number = new int[100];
		int x = 0;
	    int front = 0;
		int back = 0;
		int key = 0;
		int totalNumber = 0;
		System.out.println("Sorted array");
		while (infile.hasNext())
		     {
				number[x] = infile.nextInt();
				System.out.println(number[x]);
				x++;
			}	
		System.out.println();
		totalNumber = x;
		System.out.println("Enter the key");
		key = keyIn.nextInt();
		back = totalNumber;
		int middle = back /2;
		while (back > front && key != number[middle])
			{
				System.out.println("number[middle] = " + number[middle] + " middle = " + middle);
				if (key > number[middle])
					front  = middle + 1;
				if (key <number[middle])			
					back = middle -1;					
				middle = (back + front)/2;
			}
		if (key == number[middle])
			System.out.println(key + " was found at index "+middle);
		else
			System.out.println(key + " was not found in the list");
    }
    public static void method()
    {
    	System.out.println("hello");
    }
}


