Java 搜索方法不起作用
基本上我有一个hashmap,搜索方法缺少一个键盘。下一个键盘应该允许用户输入他们想要搜索的名称。没有错误。输出不允许用户输入要搜索的名称。奇怪的是我可以使用userInput方法,而且它工作得非常好。这是我的密码: 存储声明Java 搜索方法不起作用,java,hashmap,Java,Hashmap,基本上我有一个hashmap,搜索方法缺少一个键盘。下一个键盘应该允许用户输入他们想要搜索的名称。没有错误。输出不允许用户输入要搜索的名称。奇怪的是我可以使用userInput方法,而且它工作得非常好。这是我的密码: 存储声明 //Imports. import java.util.Scanner; //******************************************************************** public class MainApp {
//Imports.
import java.util.Scanner;
//********************************************************************
public class MainApp
{
//The Scanner is declared here for use throughout the whole MainApp.
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
new MainApp().start();
}
public void start()
{
//Create a Store named Store and add Employee's to the Store.
EmployeeStore Store = new EmployeeStore();
Store.add(new Employee ("James O' Carroll", 18,"hotmail.com"));
Store.add(new Employee ("Andy Carroll", 1171,"yahoo.com"));
Store.add(new Employee ("Luis Suarez", 7,"gmail.com"));
//********************************************************************
/*Test Code.
Store.searchByName("James O' Carroll");
Store.print();
Store.searchByEmail("gmail.com");
Employee andy = Store.searchByEmail("hotmail.com");
System.out.println(andy);
Employee employee = Store.searchByName("James O' Carroll");
if (employee != null)
{
employee.setEmployeeName("Joe");
employee.setEmployeeId(1);
employee.setEmployeeEmail("webmail.com");
Store.edit(employee);
Store.print();
}*/
//********************************************************************
int choice ;
System.out.println("Welcome to the Company Database.");
do
{
choice = MenuMethods.getMenuChoice(
"1.\tView All" +
"\n2.\tAdd" +
"\n3.\tDelete" +
"\n4.\tDelete All " +
"\n5.\tEdit" +
"\n6.\tSearch" +
"\n7.\tPrint"+
"\n8.\tExit", 8, "Please enter your choice:", "Error [1,8] Only");
//String temp = keyboard.nextLine(); This prevented entering the choice.
switch (choice)
{
case 1:
System.out.println("View All");
Store.print();
break;
case 2:
System.out.println("Add");
Employee employee = MenuMethods.userInput();
Store.add(employee);
break;
case 3:
System.out.println("Delete");
//Store.delete();
break;
case 4:
System.out.println("Delete All");
Store.clear();
break;
case 5:
System.out.println("Edit");
Employee employee2 = MenuMethods.userInput();
Store.searchByName(employee2.getEmployeeName());
if (employee2 != null)
{
employee2.setEmployeeName("Joe");
employee2.setEmployeeId(1);
employee2.setEmployeeEmail("webmail.com");
Store.edit(employee2);
Store.print();
}
break;
case 6:
System.out.println("Search");
Employee employee1 = MenuMethods.userInputByName();
Store.searchByName(employee1.getEmployeeName());
break;
case 7:
System.out.println("Print");
Store.print();
break;
case 8:
System.out.println("Exit");
break;
}
} while (choice != 8);
}
}
//Imports
import java.util.Scanner;
//********************************************************************
public class MenuMethods
{
private static Scanner keyboard = new Scanner(System.in);
//Methods for the Company Application menu.
//Method for validating the choice.
public static int getMenuChoice(String menuString, int limit, String prompt, String errorMessage)
{
System.out.println(menuString);
int choice = inputAndValidateInt(1, limit, prompt, errorMessage);
return choice;
}
//********************************************************************
//This method is used in the getMenuChoice method.
public static int inputAndValidateInt(int min, int max, String prompt, String errorMessage)
{
int number;
boolean valid;
do {
System.out.print(prompt);
number = keyboard.nextInt();
valid = number <= max && number >= min;
if (!valid) {
System.out.println(errorMessage);
}
} while (!valid);
return number;
}
//********************************************************************
public static Employee userInput()
{
String temp = keyboard.nextLine();
Employee e = null;
System.out.println("Please enter the Employee Name:");
String employeeName = keyboard.nextLine();
System.out.println("Please enter the Employee ID:");
int employeeId = keyboard.nextInt();
temp = keyboard.nextLine();
System.out.println("Please enter the Employee E-mail address:");
String employeeEmail = keyboard.nextLine();
return e = new Employee(employeeName , employeeId, employeeEmail);
}
//********************************************************************
public static Employee userInputByName()
{
Employee e = null;
System.out.println("Please enter the Employee Name:");
String employeeName = keyboard.nextLine();
System.out.println("Please enter the Employee Name:");
return e = new Employee(employeeName);
}
//********************************************************************
}
实际的搜索方法
public Employee searchByName(String employeeName)
{
Employee employee = map.get(employeeName);
System.out.println(employee);
return employee;
}
//********************************************************************
userInputByName方法。这就是问题所在
public static Employee userInput()
{
String temp = keyboard.nextLine();
Employee e = null;
System.out.println("Please enter the Employee Name:");
String employeeName = keyboard.nextLine();
System.out.println("Please enter the Employee ID:");
int employeeId = keyboard.nextInt();
temp = keyboard.nextLine();
System.out.println("Please enter the Employee E-mail address:");
String employeeEmail = keyboard.nextLine();
return e = new Employee(employeeName , employeeId, employeeEmail);
}
//********************************************************************
public static Employee userInputByName()
{
Employee e = null;
System.out.println("Please enter the Employee Name:");
String employeeName = keyboard.nextLine();
System.out.println("Please enter the Employee Name:");
return e = new Employee(employeeName);
}
//********************************************************************
让我尽力吧
首先,为什么在userInputByName()
中有两个println
语句
然后,在6
的情况下,只需调用方法searchByName
,而不检查搜索是否成功。我建议:
case 6:
System.out.println("Search");
Employee employee1 = MenuMethods.userInputByName();
Employee foundEmployee = Store.searchByName(employee1.getEmployeeName());
if (foundEmployee != null) {
System.out.println("Found employee!");
} else {
System.out.println("Not Found!");
}
break;
你能在初始化地图(mainApp)的地方发布代码吗?你的问题是,当使用
userInputByName()
时,你不能输入员工的姓名,但如果使用userInput()
,它会起作用吗?实际上,你发布的代码很难解决问题。我建议你发布你的主应用程序和MenuMethods
classes。因为我看到,keyboard
是EmployeeStore
的一个实例变量,但它在userInput()和userInputByName()中使用
哪些是菜单方法的方法
。好的,没问题,我现在就编辑它们。这两个打印只是为了看看它是否真的通过键盘调用。它仍然跳过键盘。从视觉上看,它与UserInput方法完全相同,所以我不明白它是如何运行的。
public static Employee userInputByName()
{
Employee e = null;
System.out.println("Please enter the Employee Name:");
String employeeName = keyboard.nextLine();
System.out.println("Please enter the Employee Name:"); //!???
return e = new Employee(employeeName);
}
case 6:
System.out.println("Search");
Employee employee1 = MenuMethods.userInputByName();
Employee foundEmployee = Store.searchByName(employee1.getEmployeeName());
if (foundEmployee != null) {
System.out.println("Found employee!");
} else {
System.out.println("Not Found!");
}
break;