I'm working on a program that searches through an array to match a String
. I currently have it set so that when a there is no match the following is printed: No record has been found
. My problem is that it prints that text in every iteration. How can i change it so it prints it only once? heres my code:
public static Employee[] searchWithId(Employee[] list, String search) {
System.out.println("Searching for the id number: " + search);
Employee[] filteredEmployees = new Employee[list.length];
int index = 0;
for (Employee list1 : list) {
if (list1.getIdNumber().equals(search)) {
System.out.println("Found id number: " + search);
filteredEmployees[index++] = list1;
String filtered = Arrays.toString(filteredEmployees).replace("[","")
.replace("]","").replace("null", "").replace(",", "");
System.out.println(filtered);
} else if (!(list[index].getIdNumber().equals(search))) {
System.out.println("No record has been found for the id number: " + search);
}
}
return Arrays.copyOfRange(filteredEmployees, 0,index);
}
Desired output:
Searching for the id number: P102432
No record has been found for the id number: P102432
Current output:
Searching for the id number: P102432
No record has been found for the id number: P102432
No record has been found for the id number: P102432
No record has been found for the id number: P102432
No record has been found for the id number: P102432
No record has been found for the id number: P102432
No record has been found for the id number: P102432
thanks in advance!
The short and direct answer:
Because your print statement is contained in your loop, it will print out however many times your loop iterates. Creating a boolean to facilitate whether or not the value has been found (and then break
ing out of the loop) will suffice to have the message print out; the concept of this has been pointed out in this answer .
However, with Java 8, there are several advantages you can get with a rewrite:
filter
elements out based on a condition.List<Employee>
(which you could turn back into an array if you really wanted to).Here is that code rewritten for use with Java 8.
public static Employee[] searchWithId(Employee[] list, String search) {
System.out.println("Searching for the id number: " + search);
final List<Employee> filteredEmployees = Arrays.stream(list)
.filter(e -> e.getIdNumber().equals(search))
.collect(Collectors.toList());
if(filteredEmployees.isEmpty()) {
System.out.println("No record has been found for the id number: " + search);
}
return filteredEmployees.toArray(new Employee[0]);
}
I will say that it doesn't make sense to have more than one Employee record to have the same ID, but this is something I leave to your discretion.
This should solve your problem, while looking for the employ if i found thim i exit the loop and do nothing, but if i can't find him and i got out of the loop i will print the message.
public static Employee[] searchWithId(Employee[] list, String search){
System.out.println("Searching for the id number: " + search);
Employee[] filteredEmployees = new Employee[list.length];
boolean resultFound = false;
int index = 0;
for (Employee list1 : list) {
if (list1.getIdNumber().equals(search)) {
System.out.println("Found id number: " + search);
filteredEmployees[index++] = list1;
String filtered = Arrays.toString(filteredEmployees).replace("[","").replace("]","").replace("null", "").replace(",", "");
System.out.println(filtered);
resultFound = true;
break;
}
}
if(!resultFound){
System.out.println("No record has been found for the id number: " + search);
}
return Arrays.copyOfRange(filteredEmployees, 0,index);
}
Added a bool to check if found/not.
public static Employee[] searchWithId(Employee[] list, String search){
System.out.println("Searching for the id number: " + search);
Employee[] filteredEmployees = new Employee[list.length];
boolean recordExist = false;
int index = 0;
for (Employee list1 : list) {
if (list1.getIdNumber().equals(search)) {
System.out.println("Found id number: " + search);
recordExist = true;
filteredEmployees[index++] = list1;
String filtered = Arrays.toString(filteredEmployees).replace("[","").replace("]","").replace("null", "").replace(",", "");
System.out.println(filtered);
}
}
if (!recordExist)
System.out.println("No record has been found for the id number: " + search);
return Arrays.copyOfRange(filteredEmployees, 0,index);
}
This is not a good way to write java code in arrays instead use Collections but for this use case
public static Employee[] searchWithIdNew(Employee[] list, String search){
System.out.println("Searching for the id number: " + search);
Employee[] emps = new Employee[list.length];
int index=0;
boolean found = false;
for(int j=0;j<list.length;j++) {
Employee emp = list[j];
if(emp.getIdNumber().equals(search)) {
System.out.println("Found id : "+ search+" at index :"+j);
emps[index++] = emp;
found=true;
}
}
if(!found) {
System.out.println("No record has been found for the id number: " + search);
}
return emps;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.