1

DeleteByName メソッドでエラーが発生しました。エラーは次のとおりです。

StudentStore 型のメソッド delete(Student) は、引数 (String) には適用されません。

これは、メソッド自体のパラメーターが間違っていることに関係していることはわかっていますが、修正方法がわかりません。そして、その後、学生を実際にテキストファイルから削除することはできません。ストアは Arraylist を使用して作成されます。5 人の学生が宣言されており、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;

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

メニューメソッド

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

    }
//---------------------------------------------------------------------------------------
}
4

4 に答える 4

3

間違ったパラメーター (文字列) で deleteByName メソッドを呼び出します

details.deleteByName(studentDelete.getStudentName());

しかし、それは学生を期待しています

public boolean deleteByName(Student s)

次のように変更する必要があります。

public boolean deleteByName(String name){

2 番目の問題は、削除プロセス自体です。オブジェクトを削除する必要があります:

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;
}
于 2012-08-11T12:44:14.020 に答える
2
details.deleteByName(studentDelete.getStudentName());

上記の行で、渡す文字列型。

削除方法を変更する

public boolean deleteByName(String s) 
    {
        if(students.remove(s))
            return students.remove(s);
            else
            return false;
}
于 2012-08-11T09:32:54.820 に答える
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;
于 2012-08-11T12:30:53.373 に答える
0

どのようなエラーが発生するかについては正確には言及していません。

コードに関する多くの問題:

  • deleteByName メソッドのシグネチャは ですがdeleteByName(Student s)、次のように呼び出します:deleteByName(studentDelete.getStudentName())実際のパラメーターは文字列のようです。問題は、deleteByName メソッドの宣言にあります。その名前が与えられた場合、パラメーターとして名前を期待していると宣言する必要があります (おそらく文字列)。

    public boolean deleteByName(String name);
    
  • 同じ名前を 2 回削除します。

    if(students.remove(s))
        return students.remove(s);
        else
        return false;
    

    これを試して:

    return students.remove(s) != null;
    
于 2012-08-11T09:36:49.763 に答える