Car Talk Lab

<< Cryptography Lab | LabTrailIndex | Employee >>

How do you search for a name?

All the names and jobs are in the ArrayList of Employee. The Employee class has an "get" method (technically called an "accessor" method for these, like

         Employee e = new Employee ("Oscar Muppet", "Trash Talker");
         String full=e.getName();
         String last=e.getLastName();
         String j=e.getJob();

Here e is the instance of the Employee class, and full would contain "Oscar Muppet", last would contain "Muppet", and j would contain "Trash Talker"

Here is a "tab-separated" text file of various Names and Jobs that are listed from the Car Talk radio program: Attach:CarTalkStaff.txt

  1. Make a Java Project and place the download the CarTalkStaff.txt file in that folder (If you are using Eclipse, put it in the same folder as your .class files)
  2. Make an Employee Class with fields for Full Name, Last Name, Job, Phone Number and SSN
  3. Make an Application that reads the CarTalkStaff.txt file into an ArrayList<Employee> and prints out the Job and Name of the tenth Employee
// This is the old way before there was a Scanner class in Java 5
try
{
    FileInputStream fstream = new FileInputStream("CarTalkStaff.txt");
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    while ((strLine = br.readLine()) != null)   
	       {
                   System.out.println (strLine);
	       }

    in.close();
} catch (Exception e)
{
    System.err.println("Error: " + e.getMessage());
}
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class CarTalk {
	public static void main(String args[])
	  {

	   try{
		   ArrayList<Employee> folks= readEmployeeFile(new File("CarTalkStaff.txt"));
		   //Your code here
		    Employee tenth = folks.get(9);
		    System.out.println("10. Our "+tenth.getJob()+ " is " +tenth.getFullName());
	    } catch (Exception e){
	      System.err.println("Drat!: " + e.getMessage());
	    }

	  }
	public static ArrayList<Employee> readEmployeeFile(File fromFile) throws IOException {
        Scanner in = new Scanner(fromFile);
        ArrayList<Employee> result=new ArrayList<Employee>();
        String line = null;
        while (in.hasNextLine()) {
            line = in.nextLine();
            result.add(new Employee(line.split("\t")));
        }
        in.close();
        return result;
    }
}
  1. Make your application ask for an int and then print out the Job and Name of the Employee that corresponds to that number.
  2. Make an Exception that can be caught if you try to read beyond the ArrayList (don't depend on the default 'OutOfBounds' exception -- make your own error message like "I don't have that many! Try again")
  3. Make your application ask for a String and then print out the Job and Name of all Employees that have that String somewhere in the name or job title.
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class CarTalk {
	public static void main(String args[])
	  {
       Scanner kb=new Scanner(System.in);
	   try{
		   ArrayList<Employee> folks= readEmployeeFile(new File("CarTalkStaff.txt"));

		   System.out.print("Type an search string: ");
		    ArrayList<Employee>  result = search(kb.nextLine(), folks);
		    for (Employee emp : result)
		    	System.out.println(" Our "+emp.getJob()+ " is " +emp.getFullName());
	    } catch (Exception e){
	      System.err.println("Drat!: " + e.getMessage());
	    }

	  }

	private static ArrayList<Employee> search(String target, ArrayList<Employee> folks) 
        {
		//your code here
	}

	public static ArrayList<Employee> readEmployeeFile(File fromFile) throws IOException {
        Scanner in = new Scanner(fromFile);
        ArrayList<Employee> result=new ArrayList<Employee>();
        String line = null;
        while (in.hasNextLine()) {
            line = in.nextLine();
            result.add(new Employee(line.split("\t")));
        }
        in.close();
        return result;
    }
}