简体   繁体   中英

My method from another class, will not call an input in my Main class

I'm teaching myself Java, and I'm trying to run a piece of code I made. I am at the point in my Udemy Self learning course where we are talking about classes. I made a project in my IDE IntelliJ, called Person. Essentially the goal is to input a first and last name as a string, and an age as a number, it then runs through and spits out the persons full name and if they are a teenager.

I however am at the input stage, and I keep getting an error message in my Main Class. When I try to input Dan however, (person.SetFirstName("Dan")), it comes back with an error saying, "setFirstName() in person cannot be applied to java.lang.String"

Below you'll find my code from the main class, and the first few lines of my person class, I feel like if I can figure out the issue with the first method, I can fix the rest.

I've asked in my class forum, but I can't seem to get an answer, some people think that it might be an issue with my IDE itself?

public class Main {

public static void main(String[] args) {

Person person = new Person();
person.setFirstName("Dan");
person.setLastName();
person.setAge();
    System.out.println("Full Name = " + person.getFullName());
    System.out.println("Are they a teenager? " + person.isTeen());
    person.setFirstName();
    person.setAge();
    System.out.println("Full Name = " + person.getFullName());
    System.out.println("Are they a teenager? " +person.isTeen());
    person.setLastName();
    System.out.println("Full Name = " + person.getFullName());

}

}

public class Person {

private String firstName;
private String lastName;
private int age;

public String getFirstName(){
    return this.firstName;
}
public void setFirstName(){
    this.firstName = firstName;
}

public void setFirstName() doesn't have any parameters defined, but you are passing string argument to it ( person.setFirstName("Dan"); ) - thus the error.

Try changing method signature to:

public void setFirstName(String firstName){
        this.firstName = firstName;
    }

Change this

public void setFirstName(){
    this.firstName = firstName;
}

to

public String setFirstName(){
    this.firstName = firstName;
    return firstName;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM