Reputation: 5
I am looking for a bit of help searching within an array using startsWith().
I want the user to enter a first name, search the array, and if the first name matches, print: employee ID number, last name, first name.
If there are no matches, return a simple error message.
The code below searches through the array, prints the matching employees, but also continues to print the error message until it has looped through the whole array.
The output currently:
ID No: 2345 - Holt, Steve
ID No: 2345 - Molt, Steve
ID No: 2345 - Sholt, Steve
ID No: 2345 - Colt, Steve
No Matching employee records found!
No Matching employee records found!
No Matching employee records found!
No Matching employee records found!
...
How can I change it to only return matches, OR if not matches, only then return the error message.
Thanks for your help!
private static final Service [] employeeList = new Employee [10];
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args){
// First name / Last name / Employee Number
final Employee FakeEmployee0 = new Employee("Steve", "Holt", "2345");
final Employee FakeEmployee1 = new Employee("Steve", "Molt", "2111");
final Employee FakeEmployee2 = new Employee("Steve", "Sholt", "245");
final Employee FakeEmployee3 = new Employee("Steve", "Colt", "2222");
final Employee FakeEmployee4 = new Employee("Steve", "Wolt", "25");
final Employee FakeEmployee5 = new Employee("Boy", "Blue", "1");
final Employee FakeEmployee6 = new Employee("Boy", "Wonder", "8999");
final Employee FakeEmployee7 = new Employee("Boy", "George", "500");
final Employee FakeEmployee8 = new Employee("Will", "Smith", "123");
final Employee FakeEmployee9 = new Employee("Will", "Ferret", "23");
employeeList [0] = FakeEmployee0;
employeeList [1] = FakeEmployee1;
employeeList [2] = FakeEmployee2;
employeeList [3] = FakeEmployee3;
employeeList [4] = FakeEmployee4;
employeeList [5] = FakeEmployee5;
employeeList [6] = FakeEmployee6;
employeeList [7] = FakeEmployee7;
employeeList [8] = FakeEmployee8;
employeeList [9] = FakeEmployee9;
// Search for employees by first name:
private static void searchNames(){
System.out.println("Enter employee's first name: ");
String nameCheck = sc.nextLine();
for (int i = 0; i < employeeList.length; i++){
if (employeeList[i].getFirstName().startsWith(nameCheck)){
System.out.println("ID No: " + employeeList[i].getIdNum() + " - "
+ employeeList[i].getLastName() + " " +
employeeList[i].getFirstName());
}
else {
System.out.println("No Matching employee records found!");
}
}
}
Upvotes: 0
Views: 1635
Reputation: 5684
You can do this easily using Java 8 streams:
public static void main(final String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
Employee[] employees = {
new Employee("Steve", "Holt", "2345"),
new Employee("Steve", "Molt", "2111"),
new Employee("Steve", "Sholt", "245"),
new Employee("Steve", "Colt", "2222"),
new Employee("Steve", "Wolt", "25"),
new Employee("Boy", "Blue", "1"),
new Employee("Boy", "Wonder", "8999"),
new Employee("Boy", "George", "500"),
new Employee("Will", "Smith", "123"),
new Employee("Will", "Ferret", "23")
};
String prefix = scanner.nextLine();
List<Employee> matches =
Arrays.stream(employees).filter(e -> e.getFirstName().startsWith(prefix)).collect(Collectors.toList());
if (matches.isEmpty()) {
System.out.println("No Matching employee records found!");
} else {
matches.stream().forEach(System.out::println);
}
scanner.close();
}
If you can't use streams, you have to iterate over the array and build the matches
list on your own.
I moved the
System.out.println("ID No: " + employeeList[i].getIdNum() + " - "
+ employeeList[i].getLastName() + " " +
employeeList[i].getFirstName());
part into the toString
method of the Employee
class like this:
public String toString() {
return String.format("ID No: %s - %s %s", id, lastName, firstName);
}
Thus we are able to write just matches.stream().forEach(System.out::println);
to print all matching employees.
Upvotes: 0
Reputation: 52185
The problem is in your for
loop. You are essentially printing the error message for each employee which does not fit your description.
To solve this issue, you can simply have a boolean flag, call is empFound
which is initially set to false
. As you go through your employees, if a match is found, then you simply set empFound
to true and break
the loop.
Outside the for
loop, you show the error message if and only if empFound
is false
.
As @vvs pointed out, the solution proposed here will stop once that you have found your match, that is, it will yield at most 1 hit. If you want to list all the employees which start with the string you are after, simply omit the break
statement. This will cause the loop to keep going and check all your employee elements.
Upvotes: 5
Reputation: 3785
Declare a flag isFound = false;
Add a break :
if (employeeList[i].getFirstName().startsWith(nameCheck)){
System.out.println("ID No: " + employeeList[i].getIdNum() + " - "
+ employeeList[i].getLastName() + " " +
employeeList[i].getFirstName());
isFound = true;
break;
}
And remove the error message from the loop. Outside of the loop check :
if(isFound) {
// print message
}
Upvotes: -2
Reputation: 2514
I think he wants to print all employess that match thus break will not solve it. What you should do is have a flag if you found at least one employee with given name.
boolean atLeastOneFound = false;
for (int i = 0; i < employeeList.length; i++){
if (employeeList[i].getFirstName().startsWith(nameCheck)){
System.out.println(...);
atLeastOneFound = true;
}
}
if (!atLeastOneFound) {
System.out.println("No Matching employee records found!");
}
Upvotes: 0
Reputation: 1513
Insert break
statement in inside your if block as your for is executing untill the loop ends
if (employeeList[i].getFirstName().startsWith(nameCheck)){
System.out.println("ID No: " + employeeList[i].getIdNum() + " - "
+ employeeList[i].getLastName() + " " +
employeeList[i].getFirstName());
break;
}
Upvotes: -1