在我的数组列表中显示我正在搜索的内容时遇到问题。当我搜索数组时,它会找到信息,但它将整个列表打印到所有名为fisherman的对象。它将搜索该项目,如果找到将打印我想要的项目,但它打印所有渔民的实例。但搜索是笨重的,在某些情况下不起作用。所以,我需要搜索功能的帮助才能正确搜索和输出该搜索。我不知道如何改变#34;渔夫"然后,每个渔民的SSN将有助于搜索参数。但我无法找到改变每个渔民的对象名称的方法。感谢您提供的任何帮助。
public static void fishermanMenu() {
// declare variables and array
int selection;
String fName;
String lName;
String ssn;
String number;
String email;
String searchSSN;
String menuFisherman[] = {"Register New Fisherman", "Search Fisherman", "Return to Main Menu"};
System.out.println("\nFisherman Menu");
// for loop to print menu from array
for(int i = 0, k = 1; i < menuFisherman.length; i++, k++) {
System.out.println("("+ k + ") " + (menuFisherman[i]));
}
System.out.print("Selection: ");
selection = input.nextInt();
input.nextLine();
if (selection == 1) {
$Fisherman fisherman = new $Fisherman();
fishermen.add(fisherman);
System.out.print("Enter First Name: ");
fName = input.next();
fisherman.setFirstName(fName);
System.out.print("Enter Last Name: ");
lName = input.next();
fisherman.setLastName(lName);
System.out.print("Enter Phone Number...ie 904-560-7896: ");
number = input.next();
fisherman.setPhoneNumber(number);
System.out.print("Enter SSN...ie 123-45-6789: ");
ssn = input.next();
fisherman.setSSN(ssn);
System.out.print("Enter email address: ");
email = input.next();
fisherman.setEmailAddress(email);
System.out.print(fishermen.toString());
fishermanMenu();
}
else if (selection == 2) {
System.out.println("Enter Social Security Number of fisherman to search for below.");
System.out.print("SSN: ");
searchSSN = input.nextLine();
for ($Fisherman fisherman : fishermen) {
if (fisherman.getSSN().equals(searchSSN)) {
System.out.println(fishermen.toString());
fishermanMenu();
}
else {
System.out.println("No match for that fisherman");
fishermanMenu();
}
}
}
}
这是输出。正如你所看到的那样,当我搜索&#34; 123&#34;它返回了整个列表,当我搜索&#34; 456&#34;它说&#34;不匹配那个渔夫&#34;。
Main Menu
(1) Fisherman Menu
(2) Tournament Menu
(3) Fish Menu
(4) Close Tournament
(5) Quit Program
Selection: 1
Fisherman Menu
(1) Register New Fisherman
(2) Search Fisherman
(3) Return to Main Menu
Selection: 1
Enter First Name: Matt
Enter Last Name: P
Enter Phone Number...ie 904-560-7896: 123
Enter SSN...ie 123-45-6789: 123
Enter email address: 123
[fisherman{SSN=123, firstName='Matt', lastName='P', phoneNumber='123', emailAddress='123'}]
Fisherman Menu
(1) Register New Fisherman
(2) Search Fisherman
(3) Return to Main Menu
Selection: 1
Enter First Name: Joe
Enter Last Name: K
Enter Phone Number...ie 904-560-7896: 456
Enter SSN...ie 123-45-6789: 456
Enter email address: 456
[fisherman{SSN=123, firstName='Matt', lastName='P', phoneNumber='123', emailAddress='123'}, fisherman{SSN=456, firstName='Joe', lastName='K', phoneNumber='456', emailAddress='456'}]
Fisherman Menu
(1) Register New Fisherman
(2) Search Fisherman
(3) Return to Main Menu
Selection: 2
Enter Social Security Number of fisherman to search for below.
SSN: 123
[fisherman{SSN=123, firstName='Matt', lastName='P', phoneNumber='123', emailAddress='123'}, fisherman{SSN=456, firstName='Joe', lastName='K', phoneNumber='456', emailAddress='456'}]
Fisherman Menu
(1) Register New Fisherman
(2) Search Fisherman
(3) Return to Main Menu
Selection: 2
Enter Social Security Number of fisherman to search for below.
SSN: 456
No match for that fisherman
Fisherman Menu
(1) Register New Fisherman
(2) Search Fisherman
(3) Return to Main Menu
Selection:
答案 0 :(得分:0)
这是因为您正在打印fishermen.toString()
这是整个列表。
System.out.println(fishermen.toString());
更改上述内容以改为打印fisherman.toString()
修改强>
您正在为循环中的每位渔夫使用if-else
,您需要将其修复如下:
int found = 0;
for ($Fisherman fisherman : fishermen) {
if (fisherman.getSSN().equals(searchSSN)) {
found = 1;
System.out.println(fisherman);
break;
}
if (found == 0) {
System.out.println("No match for that fisherman");
fishermanMenu();
}
else {
found = 0;
fishermanMenu();
}
答案 1 :(得分:0)
public class Main {
private static List<Fisherman> allFisherpeople = new ArrayList<Fisherman>();
public static void main(String... args) {
while (true) {
printMenu();
String selection = getInput();
if ("1".equals(selection)) {
addFisherman();
} else if ("2".equals(selection)) {
searchFishermen();
} else {
System.out.println("Not a vaild selection: " + selection);
}
}
}
private static String getInput() {
try {
String string = new String();
char character;
while ((character = (char) System.in.read()) != '\n')
string += character;
return string;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static void printMenu() {
String menuFisherman[] = { "Register New Fisherman", "Search Fisherman", "Return to Main Menu" };
System.out.println("\nFisherman Menu");
for (int i = 0; i < menuFisherman.length; i++) {
System.out.println("(" + (i + 1) + ") " + (menuFisherman[i]));
}
System.out.print("Selection: ");
}
private static void addFisherman() {
Fisherman fisherman = new Fisherman();
System.out.print("Enter First Name: ");
fisherman.setFirstName(getInput());
System.out.print("Enter Last Name: ");
fisherman.setLastName(getInput());
System.out.print("Enter Phone Number...ie 904-560-7896: ");
fisherman.setPhoneNumber(getInput());
System.out.print("Enter SSN...ie 123-45-6789: ");
fisherman.setSsn(getInput());
System.out.print("Enter email address: ");
fisherman.setEmail(getInput());
System.out.print(fisherman);
allFisherpeople.add(fisherman);
}
private static void searchFishermen() {
System.out.println("Enter Social Security Number of fisherman to search for below.");
System.out.print("SSN: ");
String search = getInput();
for (Fisherman fisherman : allFisherpeople) {
if (fisherman.getSsn().equals(search)) {
System.out.println("You must be looking for: " + fisherman);
} else {
System.out.println("No match for that fisherman");
}
}
}
private static class Fisherman {
private String firstName;
private String lastName;
private String ssn;
private String phoneNumber;
private String email;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString() {
return firstName + " " + lastName + " (" + phoneNumber + ")";
}
}
}
答案 2 :(得分:0)
感谢您的帮助。几个小时后,一双新鲜的眼睛和你的评论我能够看到问题。我将第44和56行的代码从(fishermen.toString())
更改为(fisherman)
,这解决了每次打印所有渔夫的问题。还将else语句更改为for循环外的单个print语句。这使得搜索能够在打印“不匹配该渔夫”之前浏览整个清单,而不仅仅是第一个渔民。
else if (selection == 2) {
System.out.println("Enter Social Security Number of fisherman to search for below.");
System.out.print("SSN: ");
searchSSN = input.nextLine();
for ($Fisherman fisherman : fishermen) {
if (fisherman.getSSN().equals(searchSSN)) {
System.out.println(fisherman);
fishermanMenu();
}
}
System.out.println("No match for that fisherman");
fishermanMenu();
}