배열을 통해 String
과 일치하는 프로그램을 검색하고 있습니다. 나는 현재 일치하지 않을 때 다음이 인쇄되도록 설정했습니다 : No record has been found
. 내 문제는 모든 반복에서 텍스트를 인쇄한다는 것입니다. 어떻게 한 번만 인쇄하도록 변경할 수 있습니까? 를 heres 내 코드 :배열을 통해 자바 루핑
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);
}
원하는 출력 :
Searching for the id number: P102432
No record has been found for the id number: P102432
전류 출력 : 사전에
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
감사합니다!
그래서 ... 'break' the loop? –
@KoosGadellaa 나는 break를 사용하려고했지만, 그때 나의 코드의 첫번째 비트는 작동하지 않았다. – Deescomaster
부울 값을 추가하여 발견되었는지 여부를 확인하고 그에 따라 적절한 메시지를 인쇄하십시오. 응답을 확인하십시오. 작동해야합니다. –