"Actual or formal argument lists differs in length"
Asked Answered
S

4

32

When I try to put something in the () brackets of Friends f = new Friends(friendsName, friendsAge); it comes up with the error:

Constructor Friends in class Friends cannot by applied to given types. Required: no arguments. Found: String, int. Reason: actual or formal argument lists differ in length.

But when I take out the arguments my friends list only displays "null 0". Are the values not set even though I have String friendsName = input.next();?

Also, when I try to remove a friend, it doesn't do anything. In the source code it does bring up a warning,

Suspicious call to util.java.Collection.remove: Given object cannot contain given instances of String (expected Friends).

I'm confused on what that all means?

import java.util.ArrayList;
import java.util.Scanner;

public class Friends
{
    public static void main( String[] args )
    {
        int menu;       
        int choice;
        choice = 0;      

        Scanner input = new Scanner(System.in);
        ArrayList< Friends > friendsList = new ArrayList<  >();       

        System.out.println(" 1. Add a Friend ");
        System.out.println(" 2. Remove a Friend ");
        System.out.println(" 3. Display All Friends ");
        System.out.println(" 4. Exit ");
        menu = input.nextInt();

        while(menu != 4)
        {    

            switch(menu)
            {                     

            case 1:

                while(choice != 2)
                {
                    System.out.println("Enter Friend's Name: ");
                    String friendsName = input.next();
                    System.out.println("Enter Friend's Age: ");
                    int friendsAge = input.nextInt();                               
                    Friends f = new Friends(friendsName, friendsAge);
                    friendsList.add(f);
                    System.out.println("Enter another? 1: Yes, 2: No");
                    choice = input.nextInt();
                } break;

            case 2:

                System.out.println("Enter Friend's Name to Remove: ");
                friendsList.remove(input.next());                   
                break;   

            case 3:

                for(int i = 0; i < friendsList.size(); i++)
                {
                    System.out.println(friendsList.get(i).name + " " + friendsList.get(i).age);                        
                } break;                
        }

        System.out.println(" 1. Add a Friend ");
        System.out.println(" 2. Remove a Friend ");
        System.out.println(" 3. Display All Friends ");
        System.out.println(" 4. Exit ");
        menu = input.nextInt();

    }

    System.out.println("Thank you and goodbye!");

}

    public String name;
    public int age;    

    public void setName( String friendsName )
    {
        name = friendsName;
    } 
    public void setAge( int friendsAge )
    {
        age = friendsAge;
    }
    public String getName()
    {
        return name;
    }
    public int getAge()
    {
        return age;
    }
}
Smart answered 19/7, 2013 at 15:4 Comment(0)
R
24

You try to instantiate an object of the Friends class like this:

Friends f = new Friends(friendsName, friendsAge);

The class does not have a constructor that takes parameters. You should either add the constructor, or create the object using the constructor that does exist and then use the set-methods. For example, instead of the above:

Friends f = new Friends();
f.setName(friendsName);
f.setAge(friendsAge);
Respirable answered 19/7, 2013 at 15:8 Comment(5)
Awesome. Thanks for the help! Do you have any idea why the "friendsList.remove(input.next());" isn't happy?Smart
You can't remove a String from a list that contains Friend objects. You'll have to iterate through the list of Friend and find the Friend that has the name the same as what was entered.Enrol
Note that I consider the "empty constructor + modifiers" an anti-pattern. I don't think that it's a good idea to be able to construct objects that can't be used, or would cause errors or other surprises until they're initialized in subsequent (possibly forgotten) steps.Enrol
I'm not a fan either but they have their uses in certain types of programs (think java beans and dtos)Respirable
I had a similar issue. But the constructor was there in another project that my project was dependent on. Eclipse did not complain but I got compile time on mvn install. I am wondering how this could be possible. Although I got it resolved using setters as mentioned in the accepted answer, I am just curious to know what was wrong.Gide
S
3

If you are working with daggger like me, such condition may occur. This meant that, first you have one parameter, and then you add second parameter to dagger constructor. So when dagger auto generate code, it gives error - " dependency injection actual and formal argument lists differ in length "

You should

  1. invalidate and caches
  2. Rebuid-project

it helps dagger to identify your new added paramter, then your project works again ))

Shutt answered 22/12, 2021 at 6:29 Comment(2)
I was using Intellij idea and rebuilding the project (from Build menu option) solved the issue for me.Nadianadine
yeah, one of above 2 ways should solve problem, happy for youShutt
E
2

The default constructor has no arguments. You need to specify a constructor:

    public Friends( String firstName, String age) { ... }
Enrol answered 19/7, 2013 at 15:8 Comment(1)
He actually does have some properties on it. They're just located above the getters/setters instead of at the top.Botulin
M
1

Say you have defined your class like this:

    @Data
    @AllArgsConstructor(staticName = "of")
    private class Pair<P,Q> {

        public P first;
        public Q second;
    }

So when you will need to create a new instance, it will need to take the parameters and you will provide it like this as defined in the annotation.

Pair<Integer, String> pair = Pair.of(menuItemId, category);

If you define it like this, you will get the error asked for.

Pair<Integer, String> pair = new Pair(menuItemId, category);
Metastasize answered 10/5, 2020 at 3:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.