Question

I'm having an errors with my DeleteByName method. The error is:

The method delete(Student) in the type StudentStore is not applicable for the arguments (String)

I know this is to do with the parameters of the method itself being wrong but I don't know how to fix it. And then I can't get the student to actually be deleted from the text file afterwards. The store is made using an Arraylist. There are five students being declared and I have the add method working.

Here is my code:

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;

    }
//---------------------------------------------------------------------------------------
}
Was it helpful?

Solution

You invoke your deleteByName Method with the wrong parameter(String)

details.deleteByName(studentDelete.getStudentName());

but it expect Student

public boolean deleteByName(Student s)

You should change it to:

public boolean deleteByName(String name){

And your second problem is the remove process itself. You have to remove an Object:

public boolean deleteByName(String name){
    Student s = new Student(name);
    return students.remove(s);
}

To remove an Object from a list, you need an equals-Method in your Student class. Without that method, remove cannot find the right Object to remove (replace name with the right attribute name from your Student-class!):

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

OTHER TIPS

details.deleteByName(studentDelete.getStudentName());

in your above line your passing String type.

change your delete method

public boolean deleteByName(String s) 
    {
        if(students.remove(s))
            return students.remove(s);
            else
            return false;
}
// ---------------------------------------------------------------------------------------
// Name: Remove.
// ---------------------------------------------------------------------------------------
     public void delete(String s)
     {
         students.remove(s);
     }

And then in the MainApp

case 3:
                        System.out.println("Delete by Name.");
                        Student studentDelete = MenuMethods.userInputByName();
                        details.searchByName(studentDelete.getStudentName());
                        details.delete(studentDelete.getStudentName());
                        break;

You didn't mention exactly what error you get.

A number of problems with your code:

  • The signature of the deleteByName method is deleteByName(Student s), but you invoke it as this: deleteByName(studentDelete.getStudentName()) whose actual parameter seems to be a String. The problem is with the declaration of the deleteByName method. Given its name, you should declare it as expecting a name as a parameter (a String most probably):

    public boolean deleteByName(String name);
    
  • You remove the same name 2 times:

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

    Try this:

    return students.remove(s) != null;
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top