Brady
Brady

Reputation: 21

Can't call a method from a separate class in Java

making a little mini blog application, just setting up the places to hold the data. I have 3 classes one for the post one for the user and one for the test, which I named Blog. when I try to call the getName(); method in the Blog class it won't run it keeps saying it needs a string, but I made an array of user objects, and input a string for the userName spot, and it still isn't working.

    public class Blog
{
    public static void main(String []args)
    {
        User[] userList = new User[3];
        userList[0] = new User("what.com", "TheLegionCrab", "Jake Parham", "[email protected]");
        userList[1] = new User("huh.com", "RaggleFraggle", "Brett Hawkins", "[email protected]");
        userList[2] = new User("buh.com", "SeanBeast", "Sean Sweeney", "[email protected]");

        for(int counter = 0; counter<userList.length; counter++)
        {
            User.getName();
        }
    }
}

public class User
{
    private String url; 
    private String userName;
    private String realName;
    private String email;

    public User(String url, String userName, String realName, String email)
    {
        this.url = url;
        this.userName = userName;
        this.realName = realName;
        this.email = email;
    } 
    public void getName(String userName)
    {
        System.out.println(userName);
    }
}

Upvotes: 2

Views: 1469

Answers (6)

AchmadJP
AchmadJP

Reputation: 903

public void getName(String userName)
{
    System.out.println(userName);
}

Here your function require a String. That is why it need string to run. If you want to print the userName of the current User object in your loop then use this.

public void getName()
{
    System.out.println(this.userName);
}

This refer to current User object in your loop. Now back to your loop.

    for(int counter = 0; counter<userList.length; counter++)
    {
        User.getName();
    }

You use User class meanwhile you create variable as

User[] userList = new User[3];

To print from your var, you should use the var.

    for(int counter = 0; counter<userList.length; counter++)
    {
        userList[counter].getName();
    }

Upvotes: 2

thepace
thepace

Reputation: 2221

Issues:

a) Calling getName() without any object i.e as a static function. Instead use the objects created and call it as:

userList[counter].getName();

b)Creating User objects with the name (in constructor) and also calling getName with argument i.e userName. This is wrong/not needed. When you created the object, you have already informed the object about the userName. So have a clean getter without any argument.

getName() 

The code:

public class Blog
{
    public static void main(String []args)
    {
        User[] userList = new User[3];
        userList[0] = new User("what.com", "TheLegionCrab", "Jake Parham", "[email protected]");
        userList[1] = new User("huh.com", "RaggleFraggle", "Brett Hawkins", "[email protected]");
        userList[2] = new User("buh.com", "SeanBeast", "Sean Sweeney", "[email protected]");


        for(int counter = 0; counter<userList.length; counter++)
        {
            userList[counter].getName(); # Correct this to use the created objects.
        }
    }
}

public class User
{
    private String url; 
    private String userName;
    private String realName;
    private String email;

    public User(String url, String userName, String realName, String email)
    {
        this.url = url;
        this.userName = userName;
        this.realName = realName;
        this.email = email;
    } 
    public void getName() #Remove the argument.
    {
        System.out.println(this.userName);
    }
}

Upvotes: 0

Nemeas
Nemeas

Reputation: 129

It looks like what you want is to print the name of the user instance instead of the parameter to the getName method. Try this defining the method like this:

    public void getName()
    {
        System.out.println(this.userName);
    }

and in the main method:

User[] userList = new User[3];
userList[0] = new User("what.com", "TheLegionCrab", "Jake Parham", "[email protected]");
userList[1] = new User("huh.com", "RaggleFraggle", "Brett Hawkins", "[email protected]");
userList[2] = new User("buh.com", "SeanBeast", "Sean Sweeney", "[email protected]");

for(int counter = 0; counter<userList.length; counter++)
{
     userList[counter].getName();
}

Upvotes: 0

brendan
brendan

Reputation: 308

That's because you specified that the getName method in the User class takes a String as the argument. You did that here:

public void getName(String userName)
{
    System.out.println(userName);
}

So it's working exactly they way you have told it to.

But really, you want "setName()" to take a String arg, and "getName()" to take no arg. It should look like this:

public void setName(String new_userName)
{
    username = new_userName;
}

public void getName()
{
    System.out.println(userName);
}

But even then, I'd say that your method names are a bit ambiguous: should I use "getName" to get the username, or to get the realName? (did you spot the error?)

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

You need to access a User instance from your userList array (or get an instance some other way) to call the method on (and either assign it to a variable and use it or just print it). In Java, array access is performed with []. Something like

for (int counter = 0; counter < userList.length; counter++)
{
    System.out.println(userList[counter].getName());
}

You could also you an enhanced for loop (for-each loop) like

for (User user : userList) {
    System.out.println(user.getName());
}

Also, I don't think getName should be shadowing the class name field. You wanted something like,

public void getName()
{
    System.out.println(this.userName);
}

or following the Java practice of returning the value in a getter (and to fix my examples above)

public String getName()
{
    return this.userName;
}

Upvotes: 0

paudel.sulav
paudel.sulav

Reputation: 162

You have to make the object of the User class before calling else your User class should be static to call it direct.

for(int counter = 0; counter<userList.length; counter++)
    {   User obj = new User();
        obj.getName();
    }

else

public static User

{

public User(String url, String userName, String realName, String email)
{
    this.url = url;
    this.userName = userName;
    this.realName = realName;
    this.email = email;
} 
public void getName(String userName)
{
    System.out.println(userName);
}

}

Upvotes: -1

Related Questions