我的DeleteByName方法出错了。错误是:
StudentStore类型中的delete(Student)方法不适用于参数(String)
我知道这与方法本身的参数错误有关,但我不知道如何修复它。之后我无法让学生真正从文本文件中删除。商店使用Arraylist制作。有五个学生被宣布,我有添加方法工作。
这是我的代码:
MainApp
//---------------------------------------------------------------------------------------
// Name: Case 3: Delete by Name.
// Description: Choice 3 gives the user an option to delete an employee by name.
//---------------------------------------------------------------------------------------
case 3:
System.out.println("Delete by Name.");
Student studentDelete = MenuMethods.userInputByName();
details.searchByName(studentDelete.getStudentName());
details.deleteByName(studentDelete.getStudentName());
break;
StudentStore
// ---------------------------------------------------------------------------------------
// Name: DeleteByName.
// ---------------------------------------------------------------------------------------
public boolean deleteByName(Student s)
{
if(students.remove(s))
return students.remove(s);
else
return false;
}
public Student searchByName(String employeeName)
{
Student employee = Student.get(employeeName);
System.out.println(employee);
return employee;
}
// ---------------------------------------------------------------------------------------
// Name: Search by Email.
// ---------------------------------------------------------------------------------------
public String searchByEmail(String studentEmail)
{
for (Student student : map.values())
{
if (student.getStudentEmail().equals(studentEmail)
{
System.out.println(student.getStudentEmail());
return student.getStudentEmail();
}
}
return null;
}
MenuMethods
//---------------------------------------------------------------------------------------
// Name: Imports.
// Description: To allow the use of different Java classes.
//---------------------------------------------------------------------------------------
import java.util.Scanner;
//---------------------------------------------------------------------------------------
public class MenuMethods
{
private static Scanner keyboard = new Scanner(System.in);
//---------------------------------------------------------------------------------------
// Methods for the Company Application menu.
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
// Name: getMenuChoice.
// Description: 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;
}
//---------------------------------------------------------------------------------------
// Name: inputAndValidateInt.
// Description: 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;
}
//---------------------------------------------------------------------------------------
// Name: userInput
// Description: This method is used in the MainApp to give the user capability to enter
// the details when adding details of an employee into the store.
//---------------------------------------------------------------------------------------
public static Student userInput()
{
String temp = keyboard.nextLine();
Student s = null;
System.out.println("Please enter the Student Name:");
String studentName = keyboard.nextLine();
System.out.println("Please enter the Student ID:");
String studentId = keyboard.nextLine();
System.out.println("Please enter the Student E-mail address:");
String studentEmail = keyboard.nextLine();
System.out.println("Please enter the Student telephone number:");
String studentTelephoneNumber = keyboard.nextLine();
return s = new Student(studentName, studentId, studentEmail,studentTelephoneNumber);
}
//---------------------------------------------------------------------------------------
// Name: userInputByName.
// Description: This method is used in the MainApp to give the user capability to search by name.
//---------------------------------------------------------------------------------------
public static Student userInputByName()
{
// String temp is for some reason needed. If it is not included
// The code will not execute properly.
String temp = keyboard.nextLine();
Student s = null;
System.out.println("Please enter the Student Name:");
String studentName = keyboard.nextLine();
return s = new Student(studentName);
}
//---------------------------------------------------------------------------------------
// Name: userInputByEmail
// Description: This method is used in the MainApp to give the user capability to search by email.
//---------------------------------------------------------------------------------------
public static String userInputByEmail()
{
// String temp is for some reason needed. If it is not included
// The code will not execute properly.
String temp = keyboard.nextLine();
Student s = null;
System.out.println("Please enter the StudentEmail:");
String studentEmail = keyboard.nextLine();
// This can use the employeeName's constructor because java accepts the
// parameters instead
// of the name's.
return studentEmail;
}
//---------------------------------------------------------------------------------------
}
答案 0 :(得分:3)
使用错误的参数(String)
调用deleteByName方法details.deleteByName(studentDelete.getStudentName());
但它希望学生
public boolean deleteByName(Student s)
您应将其更改为:
public boolean deleteByName(String name){
你的第二个问题是删除过程本身。您必须删除一个对象:
public boolean deleteByName(String name){
Student s = new Student(name);
return students.remove(s);
}
要从列表中删除Object,您需要在Student类中使用equals-Method。如果没有该方法,删除无法找到要删除的正确对象(用您的学生类中的正确属性名称替换名称!):
public boolean equals(Object b){
if(this.name.equals(b.name)){
return true;
}
return false;
}
答案 1 :(得分:2)
details.deleteByName(studentDelete.getStudentName());
在您的上一行中传递String类型。
更改您的删除方法
public boolean deleteByName(String s)
{
if(students.remove(s))
return students.remove(s);
else
return false;
}
答案 2 :(得分:0)
你没有提到你到底发生了什么错误。
您的代码存在许多问题:
deleteByName方法的签名是deleteByName(Student s)
,但您可以这样调用它:deleteByName(studentDelete.getStudentName())
,其实际参数似乎是一个String。
问题在于deleteByName方法的声明。鉴于其名称,您应该将其声明为期望名称作为参数(最可能是String):
public boolean deleteByName(String name);
您删除相同的名称2次:
if(students.remove(s))
return students.remove(s);
else
return false;
试试这个:
return students.remove(s) != null;
答案 3 :(得分:0)
// ---------------------------------------------------------------------------------------
// Name: Remove.
// ---------------------------------------------------------------------------------------
public void delete(String s)
{
students.remove(s);
}
然后在MainApp
中case 3:
System.out.println("Delete by Name.");
Student studentDelete = MenuMethods.userInputByName();
details.searchByName(studentDelete.getStudentName());
details.delete(studentDelete.getStudentName());
break;