java - DeleteByName 方法不适用于 RandomAccessFile

标签 java arraylist delete-file random-access

我的 DeleteByName 方法出错。错误是:

StudentStore 类型中的方法 delete(Student) 不适用于参数 (String)

我知道这与方法本身的参数错误有关,但我不知道如何修复它。然后我无法让学生真正从文本文件中删除。该商店是使用 Arraylist 创建的。有五个学生被宣布,我有 add 方法。

这是我的代码:

主应用

//---------------------------------------------------------------------------------------
//          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;

学生商店

// ---------------------------------------------------------------------------------------
// 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;
}

菜单方法

//---------------------------------------------------------------------------------------
//  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;

    }
//---------------------------------------------------------------------------------------
}

最佳答案

您使用错误的参数(字符串)调用您的 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);
}

要从列表中删除对象,您需要在 Student 类中使用 equals-Method。如果没有该方法,remove 将无法找到要删除的正确对象(将 name 替换为 Student 类中正确的属性名称!):

public boolean equals(Object b){
    if(this.name.equals(b.name)){
        return true;
    }
    return false;
}

关于java - DeleteByName 方法不适用于 RandomAccessFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11913471/

相关文章:

java - 如何使用扫描仪检查用户输入而不多次提示扫描仪

java - Android - 一旦网络连接发生变化(移动数据禁用并再次启用),Paho Mqtt 客户端不接收消息

java - 如何确定 Java Android Looper 是否繁忙

java - Android 8.1 - 为什么我无法删除歌曲文件?

file - 如何删除 Google 云端硬盘中的文件?

java - Maven 正在尝试从私有(private)存储库安装每个 jar

java - 将数组列表导入并打印到 JSP

Java - 混合数据类型/打印它们

java - 在 Java 中如何递归打印 arrayList?

Android 从内部存储中删除文件文件夹不起作用