/**
 * Demonstrate searching in an unordered array for an int.
 *
 * @author	Terry Sergeant
 * @version Case Study v1.0
*/
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.File;

public class IntSearch
{
	public static void main(String [] args)
	{
		Scanner kb= new Scanner(System.in);
		int [] numbers= new int[50];
		int i,n;
		int searchVal;

		n= loadNumbers(numbers);
		displayNumbers(numbers,n);

		System.out.println("Enter a number to search for: ");
		searchVal= kb.nextInt();
		i=0;

		while(i<n && numbers[i]!=searchVal)
			i++;

		if (i >= n)
			System.out.println("Item not found");
		else
			System.out.println("Found "+searchVal+" at position "+i);

	}


	/**
	 * Loads numbers from a datafile into the given array.
	 *
	 * @param numbers an array of integers to be filled with contents from data file.
	 * @return the number of values read from the file
	*/
	public static int loadNumbers(int [] numbers)
	{
		Scanner numfile;
		int n=0;

		try
		{
			numfile= new Scanner(new File("numbers.txt"));

			while (numfile.hasNextInt())
			{
				numbers[n]= numfile.nextInt();
				n++;
			}

			numfile.close();
		}
		catch (Exception e)
		{
			System.out.println(e);
		}

		return n;
	}


	/**
	 * Displays the first n numbers in the provided array.
	 *
	 * @param numbers an array of integers to be displayed
	 * @param n the number of values to be displayed
	*/
	public static void displayNumbers(int [] numbers, int n)
	{
		int i;

		System.out.println("Array has "+n+" values. Here they are: ");
		for (i=0; i<n; i++)
			System.out.println(numbers[i]);
	}
}
