I am mostly done working on the homework assignment for my class, it requires that I complete an employee directory system that takes a user input value and compare it to 3 values employeeName, employeeID, and phoneNumber. The system must work in such a way that if the search is inconclusive the user must only enter another character to continue the search, and when the correct employee is located all data must be printed on that employee.

There are two classes here. FullTimeEmployee which contains the class to define the employee objects, and certain methods to set, and get the values for these objects, as well as the display method. Then there is EmployeeDirectorySystem which contains the main method and is responsible for all the actual manipulation and searching. If you examine the code for EmployeeDirectorySystem you will see that it displays a welcome message, then loads the data into all 12 employee objects which are contained in the array employee[]. Then the getInput() method is called which uses the scanner method readString() to obtain input from the user. Then the method runDirectory() is called, which contains the control loop for the searching functionality. The searchDirectory() method is what contains the actual functionality, and that is where I am currently stumped.

What I need to do is finish coding this method so that if the user enters Quit the system exits, if the user enters Redo the input value is reset, and otherwise it takes the input string and uses the String contains() method to search for a match in each employee object for employeeName, employeeID, and phoneNumber. If there are no matches, it should probably reset and send a message. If there is one match then that is the correct object and it should be displayed, and if there are more than one I need to tell the user to enter another character.

The currentInput is the previous input, plus any other input that the user enters, I'm still working on exactly how to code this. As for the search loop itself it should have a variable that increments everytime a match is found, I'm not sure how to set that up. It should also the first object match value to another variable, so then if that is the only match, I can just print that variable. Well something along those lines, it's a little confusing as to how to reassign the value of an object.

So basically I was looking for help as to how to write that loop, and how to make sure the input system is correctly adding a new character to the old one so the whole string is searched. Any help on that would be very much appreciated, I'm trying to figure it out but this is very hard for me at my current level of java.

EmployeeDirectorySystem

/**
 * EmployeeDirectorySystem class works as 
 * the main controlling system for 
 * accessing full time employee objects.
 * CSCE 155 Fall 2008
 * Assignment 2
 * @author
 * @version
 */

// import statements
import java.io.*;
import java.util.*;

public class EmployeeDirectorySystem  {
        
    // private data members -- variables
    FullTimeEmployee[] employee;
	 
	 private String input;
	 private String currentInput;
    
    /** Constructor
     *  initialize all private data members to appropriate values
     *  If you add more data members, you will have to initialize them here
     *  too.
     */
    public EmployeeDirectorySystem()  {  
	   
			employee = new FullTimeEmployee[13];
		
	   employee[0] = new FullTimeEmployee();
    	employee[1] = new FullTimeEmployee();
    	employee[2] = new FullTimeEmployee();
    	employee[3] = new FullTimeEmployee();
    	employee[4] = new FullTimeEmployee();
    	employee[5] = new FullTimeEmployee();
    	employee[6] = new FullTimeEmployee();
    	employee[7] = new FullTimeEmployee();
    	employee[8] = new FullTimeEmployee();
    	employee[9] = new FullTimeEmployee();
    	employee[10] = new FullTimeEmployee();
    	employee[11] = new FullTimeEmployee();
		
    	
    }  // end constructor
   
	public void getInput() {
	System.out.println("Please enter all or part of the employee name, ID, or phone number.");
	input = readString();
	}
	  
   public void searchDirectory() {
	 //This method will check for control statements and will then
	 //run a for loop to search for the employee index of the array
	 //that matches the input, it will assign this value to a variable
	 // and when counter = 1 it will terminate and the variable will
	 // be printed
	 int counter = 0;
	 if (input == "Quit") {
	 System.exit(0);
	 							}
	 else if (input == "Redo") {
	 input = "";
	 } else {
	 while ( counter 
	 
	 if (counter == 0) {
	 System.out.println("No match found");
	 System.exit(0);
	 } else if ( counter > 1) {
	 System.out.println("Please enter another character");
	    }
    
    /**
	 * This method gets input from the user and then searches the
	 * directory for matches of that input and when found displays
	 * the information about that employee
	 */
    public void runDirectory(){
        
		int i = 0;
		while ( i < 12 ) {
				searchDirectory();
		i ++;
		}
		
		//call the display method for the correct employee
		employee[2].display();

    }//end of runDirectory
	 
    
    /**
     * This method displays a welcome message to the user.
     */
    public void displayWelcome()  {
        
    	/* Display a brief welcome message about the system */
    	System.out.println("Welcome to the CSE Employee Directory");
		System.out.println("You can view information about an employee by using this system");
        
    }  // end displayWelcome
    
    /**
     * This method displays a goodbye message to the user.
     */
    public void displayGoodbye()  {
        
        System.out.println("Thank you, Goodbye!");
        
    }  // end displayGoodbye
    
    /**
     * This method loads the employee information.  
     */
    public void loadData()  {
    	employee[0].setEmployeeName("Jeffery Johnson");
    	employee[0].setEmployeeID("A001");
    	employee[0].setDateOfBirth(new GregorianCalendar(1978, Calendar.JUNE, 14));
    	employee[0].setStartDate(new GregorianCalendar(2006, Calendar.AUGUST, 1));
    	employee[0].setPhoneNumber("402-486-5104");
    	employee[0].setStreetAddress("123 14th Street");
    	employee[0].setCity("Lincoln");
    	employee[0].setState("Nebraska");
    	employee[0].setZIP("68588");
    	employee[0].setMonthlySalary(1400);
    	
    	employee[1].setEmployeeName("Jeffery Smith");
    	employee[1].setEmployeeID("B002");
    	employee[1].setDateOfBirth(new GregorianCalendar(1973, Calendar.SEPTEMBER, 12));
    	employee[1].setStartDate(new GregorianCalendar(1999, Calendar.JANUARY, 3));
    	employee[1].setPhoneNumber("402-357-5104");
    	employee[1].setStreetAddress("321 4th Street");
    	employee[1].setCity("Seward");
    	employee[1].setState("Nebraska");
    	employee[1].setZIP("68801");
    	employee[1].setMonthlySalary(1350);
    	
    	employee[2].setEmployeeName("Jessica Smith");
    	employee[2].setEmployeeID("A303");
    	employee[2].setDateOfBirth(new GregorianCalendar(1974, Calendar.NOVEMBER, 21));
    	employee[2].setStartDate(new GregorianCalendar(2003, Calendar.FEBRUARY, 14));
    	employee[2].setPhoneNumber("402-408-6693");
    	employee[2].setStreetAddress("220 Wenzel Circle");
    	employee[2].setCity("Eagle");
    	employee[2].setState("Nebraska");
    	employee[2].setZIP("68347");
    	employee[2].setMonthlySalary(2200);
    	
    	employee[3].setEmployeeName("Jeremy Suing");
    	employee[3].setEmployeeID("X001");
    	employee[3].setDateOfBirth(new GregorianCalendar(1975, Calendar.JUNE, 5));
    	employee[3].setStartDate(new GregorianCalendar(2005, Calendar.MAY, 16));
    	employee[3].setPhoneNumber("402-472-1658");
    	employee[3].setStreetAddress("640 N. 14th Street");
    	employee[3].setCity("Lincoln");
    	employee[3].setState("Nebraska");
    	employee[3].setZIP("68588");
    	employee[3].setMonthlySalary(50);
    	
    	employee[4].setEmployeeName("Sally Smith");
    	employee[4].setEmployeeID("B001");
    	employee[4].setDateOfBirth(new GregorianCalendar(1980, Calendar.JANUARY, 11));
    	employee[4].setStartDate(new GregorianCalendar(2006, Calendar.AUGUST, 1));
    	employee[4].setPhoneNumber("402-357-3798");
    	employee[4].setStreetAddress("55894 893rd Road");
    	employee[4].setCity("Fremont");
    	employee[4].setState("Nebraska");
    	employee[4].setZIP("68321");
    	employee[4].setMonthlySalary(1400);
    	
    	employee[5].setEmployeeName("John Johnson");
    	employee[5].setEmployeeID("B010");
    	employee[5].setDateOfBirth(new GregorianCalendar(1979, Calendar.JUNE, 14));
    	employee[5].setStartDate(new GregorianCalendar(2000, Calendar.AUGUST, 1));
    	employee[5].setPhoneNumber("402-486-5110");
    	employee[5].setStreetAddress("123 14th Street");
    	employee[5].setCity("Lincoln");
    	employee[5].setState("Nebraska");
    	employee[5].setZIP("68588");
    	employee[5].setMonthlySalary(1450);
    	
    	employee[6].setEmployeeName("Sarah Smith");
    	employee[6].setEmployeeID("B200");
    	employee[6].setDateOfBirth(new GregorianCalendar(1974, Calendar.OCTOBER, 14));
    	employee[6].setStartDate(new GregorianCalendar(1997, Calendar.JANUARY, 31));
    	employee[6].setPhoneNumber("402-408-5104");
    	employee[6].setStreetAddress("321 4th Street");
    	employee[6].setCity("Utica");
    	employee[6].setState("Nebraska");
    	employee[6].setZIP("68701");
    	employee[6].setMonthlySalary(1550);
    	
    	employee[7].setEmployeeName("John Doe");
    	employee[7].setEmployeeID("A678");
    	employee[7].setDateOfBirth(new GregorianCalendar(1981, Calendar.MARCH, 28));
    	employee[7].setStartDate(new GregorianCalendar(1998, Calendar.MARCH, 2));
    	employee[7].setPhoneNumber("303-804-1169");
    	employee[7].setStreetAddress("6009 Kingsley Ave.");
    	employee[7].setCity("Littleton");
    	employee[7].setState("Colorado");
    	employee[7].setZIP("80104");
    	employee[7].setMonthlySalary(4100);
    
    	employee[8].setEmployeeName("Tad Jacobs");
    	employee[8].setEmployeeID("X222");
    	employee[8].setDateOfBirth(new GregorianCalendar(1975, Calendar.JULY, 1));
    	employee[8].setStartDate(new GregorianCalendar(2003, Calendar.AUGUST, 16));
    	employee[8].setPhoneNumber("719-555-5555");
    	employee[8].setStreetAddress("7979 Technology Way");
    	employee[8].setCity("Denver");
    	employee[8].setState("Colorado");
    	employee[8].setZIP("80237");
    	employee[8].setMonthlySalary(3333);
    	
    	employee[9].setEmployeeName("Ella Roberts");
    	employee[9].setEmployeeID("B501");
    	employee[9].setDateOfBirth(new GregorianCalendar(1983, Calendar.SEPTEMBER, 4));
    	employee[9].setStartDate(new GregorianCalendar(2006, Calendar.MAY, 10));
    	employee[9].setPhoneNumber("402-472-1100");
    	employee[9].setStreetAddress("5567 Adams Street");
    	employee[9].setCity("Lincoln");
    	employee[9].setState("Nebraska");
    	employee[9].setZIP("68521");
    	employee[9].setMonthlySalary(2400);
    	
    	employee[10].setEmployeeName("Ellen Smith");
    	employee[10].setEmployeeID("A501");
    	employee[10].setDateOfBirth(new GregorianCalendar(1982, Calendar.AUGUST, 24));
    	employee[10].setStartDate(new GregorianCalendar(2001, Calendar.MARCH, 19));
    	employee[10].setPhoneNumber("402-472-2200");
    	employee[10].setStreetAddress("567 9th Street");
    	employee[10].setCity("Lincoln");
    	employee[10].setState("Nebraska");
    	employee[10].setZIP("68520");
    	employee[10].setMonthlySalary(3400);
    	
    	employee[11].setEmployeeName("Bobby Smith");
    	employee[11].setEmployeeID("B555");
    	employee[11].setDateOfBirth(new GregorianCalendar(1983, Calendar.DECEMBER, 4));
    	employee[11].setStartDate(new GregorianCalendar(2006, Calendar.OCTOBER, 10));
    	employee[11].setPhoneNumber("402-470-1200");
    	employee[11].setStreetAddress("9876 34th Ave.");
    	employee[11].setCity("Lincoln");
    	employee[11].setState("Nebraska");
    	employee[11].setZIP("68527");
    	employee[11].setMonthlySalary(240);
    	
    }  // end loadData
    
    /**
     * This method reads an integer and
     * returns that integer to the caller of this method.
     */
    private int readInteger()  {
        
        int temp = 0;
        
        Scanner scanner = new Scanner(System.in);
        
        try {
            temp = scanner.nextInt();  // read integer
        }  catch (InputMismatchException ex) {
            System.out.println(ex);
        }
        
        return temp;
        
    }  // end readInteger
    
    /**
     * This method reads in a string and returns that string to the caller of
     * this method.
     */
    private String readString()  {
        
        String userInput = "";
        
        Scanner scanner = new Scanner(System.in);
        
        userInput = scanner.nextLine();
        
        return userInput;
        
    }  // end readString

    /**
     * This main method creates an object of the EmployeeDirectorySystem class 
     * and runs the Employee Directory System. 
     *
     * First, it asks the EmployeeDirectorySystem object to display a welcome 
     * message. Then it loads the employee data. Next, it asks the 
     * EmployeeDirectorySystem object to run the directory. Finally, it asks 
     * the EmployeeDirectorySystem object to display a goodbye message.
     */
    public static void main(String[] args)  {

    	EmployeeDirectorySystem mySystem = new EmployeeDirectorySystem();
    	mySystem.displayWelcome();
    	mySystem.loadData();
		mySystem.getInput();
    	mySystem.runDirectory();
    	mySystem.displayGoodbye();
                                     
    }  // end main
}  // end Class EmployeeDirectorySystem

FullTimeEmployee

/*
 * FullTimeEmployee.java
 *
 * CSCE 155 Fall 2008
 * Assignment 2
 * @author
 * @version
 */

// import statements
import java.util.*;

/**
 * Used to store information of the full time employee.  It can also be used
 * to calculate the pay for a full time employee.
 * 
 * @author
 * @version
 */

public class FullTimeEmployee {
    
    // -------------------------------------------------------------------------
    // You may add more data members to the following to describe a Full Time 
	// Employee.  You may need to remove some data members as well.
    // -------------------------------------------------------------------------

	//------------------Declare Constants---------------------------------------
    
    /**
     * Represents the percentage of the federal tax
     */
    private final double FEDERAL_TAX_PERCENT = 0.15; 
    
    /**
     * Represents the percentage of the health insurance deduction
     */
    private final double HEALTH_INSURANCE_PERCENT = 0.05;   

    /**
     * Represents the percentage of the Retirement Savings deduction
     */
    private final double RETIREMENT_SAVINGS_PERCENT = 0.12;   

    /**
     * Represents the percentage of the state tax
     */
    private final double STATE_TAX_PERCENT = 0.08;   
    
    //-------------Declare private variables-----------------------------------
    
    /**
     *variables used to hold the values for various information
	  *about the employee including name, ID, phone number, street address,
	  *city, state, and zip code
     */
    private String 	employeeName; 	
	 private String	employeeID;
	 private String phoneNumber;
	 private String streetAddress;
	 private String city;
	 private String state;
	 private String zip;
	 
    /**
     * Represents the amount in dollars of the salary for the employee
     */
    private int 	monthlySalary; 	
    /**
     * Represents the date of employment
     */
    private GregorianCalendar 	startDate; 
	 /**
	 * Represents the birth date for the employee
	 */		
	 private GregorianCalendar		dateOfBirth;
	 /**
	 * Various variables used to calculate pay deductions for the method
	 *calculateNetMonthly() used to calculate net pay for the employee
	 */
	 	  private double deductionFederalTax;
		  private double deductionStateTax;
		  private double deductionRetirement;
		  private double deductionHealth;
        
    /**
     * Constructor used to create this object.  Responsible for setting
     * all of this object's information to their corresponding default values
     */
    public FullTimeEmployee(){
        this.employeeName = "";
        this.employeeID = "";
		  this.phoneNumber = "";
		  this.streetAddress = "";
		  this.city = "";
		  this.state = "";
		  this.zip = "";
    }//end of constructor
    
    /**
     * Returns the employee's name from this object 
     * @return <code>String</code> Name of the Employee represented in object
     */
	public String getEmployeeName() {
		return employeeName;
	}
	public String getEmployeeID() {
		return employeeID;
	}
	public String getPhoneNumber() {
		return phoneNumber;
	}
	public String getStreetAddress(String streetAddress) {
		return streetAddress;
	}
	public String getCity(String city) {
		return city;
	}
	public String getState(String state) {
		return state;
	}
	public String getZIP(String zip) {
		return zip;
	}
	
	public GregorianCalendar getDateOfBirth(GregorianCalendar dateOfBirth) {
		return dateOfBirth;
	}
	public GregorianCalendar getStartDate(GregorianCalendar startDate) {
		return startDate;
	}

	public int getMonthlySalary(int monthlySalary) {
		return monthlySalary;
	}
    /**
     * Sets the employee name represented by this object
     * @param employeeName the name of the employee
     */
	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}
	public void setEmployeeID(String employeeID) {
		this.employeeID = employeeID;
	}
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	public void setStreetAddress(String streetAddress) {
		this.streetAddress = streetAddress;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public void setState(String state) {
		this.state = state;
	}
	public void setZIP(String zip) {
		this.zip = zip;
	}
	
	public void setDateOfBirth(GregorianCalendar dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}
	public void setStartDate(GregorianCalendar startDate) {
		this.startDate = startDate;
	}
	
	public void setMonthlySalary(int monthlySalary) {
		this.monthlySalary = monthlySalary;
	}

    /**
	 * calculateNetMonthly calculates the net pay for the employee by
	 * subtracting the deductions for Federal Tax, State Tax, Retirement
	 * savings, and health insurance from the gross pay
	 */
    public double calculateNetMonthly(){
        deductionFederalTax = (monthlySalary * FEDERAL_TAX_PERCENT);
		  deductionStateTax = (monthlySalary * STATE_TAX_PERCENT);
		  deductionRetirement = (monthlySalary * RETIREMENT_SAVINGS_PERCENT);
		  deductionHealth = (monthlySalary * HEALTH_INSURANCE_PERCENT);
		  
        double netPay = 0.0;
    	return netPay;
    }//end of calculateNetMonthly
    
	 /**
	 * This method) prints out all information about the employee
	 */
    public void display(){
        System.out.println("Displaying Information for Employee:");
		  System.out.println("Employee ID: " + employeeID);
		  System.out.println("Employee name: " + employeeName);
		  System.out.println("Date of birth: " + dateOfBirth);
		  System.out.println("Date of employment: " + startDate);
		  System.out.println("Employee phone number: " + phoneNumber);
		  System.out.println("Employee street address: " + streetAddress);
		  System.out.println("Employee city, state, and zip: " + city + ", " + state + " " + zip);
		  System.out.println("The gross pay of the employee is: " + monthlySalary);
		  
		  
    }//end of display      
}//end of class FullTimeEmployee

Well I've been working on the searchDirectory() method some more, and I'm making some small progress. But I'm still not really understanding how to proceed. Nothing is working like it's supposed to. The code compiles fine, and then I run it and if I type Quit nothing happens, I type Redo, and nothing happens, and if I type anything nothing happens. Which is really bothering me because now I have no idea where to go from here. Somewhere in there it needs to pass each employee object through comparison statements, and search for a match

public void searchDirectory() {
	 //This method will check for control statements and will then
	 //run a for loop to search for the employee index of the array
	 //that matches the input, it will assign this value to a variable
	 // and when counter = 1 it will terminate and the variable will
	 // be printed
	 int counter = 0;
	 int k = 0;
	 if (input == "Quit") {
	 System.exit(0);
	 					}
	 else if (input == "Redo") {
	 input = "";
	 } else {
	 while ( k < 12 ) {
	  if (employee[k].getEmployeeName().contains(input) == true) {
           
	 k++;
	 counter++;
	 }
	 }
	 if (counter == 0) {
	 System.out.println("No match found");
	 System.exit(0);
	 } else if ( counter > 1) {
	 System.out.println("Please enter another character");

	    }
		 }
		 }

Use ".equals()" to compare strings - not "=="

(input.equals("Quit"))

or to avoid potential null pointer exceptions

("Quit".equals(input))

. You also don't need to test booleans for "== true". The boolean, or boolean expression, is true or false by itself

if ( employee[k].getEmployeeName().contains(input) )

Well after using that Quit works, and Redo works sort of, it ends the whole thing like Quit which wasn't quite the point I'll just have to find a different way to reset the variable.

I'm still not having any luck getting the thing to loop through and compare strings.

This article has been dead for over six months. Start a new discussion instead.