/**
 * 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 StringSearch
{
	public static void main(String [] args)
	{
		Scanner kb= new Scanner(System.in);
		String [] names= new String[50];
		int i,n;
		String searchVal;

		n= loadNames(names);
		displayNames(names,n);

		System.out.println("Enter a name to search for: ");
		searchVal= kb.nextLine();

		// fourth try ... matches part of a string ... but case-sensitive
		for (i=0; i<n; i++)
			if (names[i].toLowerCase().indexOf(searchVal.toLowerCase())>=0)
				System.out.println("Found "+names[i]+" at position "+i);
		/*
		*/

		// What we really want is to have a case-insensitive .indexOf()
		// Sadly, there is no such method!
		//
		// HINT: use .toLowerCase() on both the array element and the
		//       search value so that when you compare there are only
		//       lowercase characters being compared! Try your solution
		//       to that here.
	}


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

		try
		{
			namefile= new Scanner(new File("names.txt"));

			while (namefile.hasNextLine())
			{
				names[n]= namefile.nextLine();
				n++;
			}

			namefile.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 displayNames(String [] names, int n)
	{
		int i;

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