0

I'm trying to assign a value to a variable in another class using input from the user. My goal is to use the "fullName" setter value that was called in the secondary class but I keep getting the error "cannot resolve symbol fullName". I think it may be because the variable in the other class is private, but I edited the visibility to public and that didn't change anything. How can I do this?

Here's my code:

public class Main{

    public static void main(String[] args) {

     Scanner scan = new Scanner(System.in);
     System.out.println("Enter your full name:\n");
     fullName = scan.nextLine();
    }
}

// Secondary class
public class Person {

    private int id;
    private String fullName;

    // Constructor
    public Person(String fullName, String id){

    }

    // Setters
    public static void setName(String fullName){
        this.fullName = fullName;
    }

    public static void setid(int id){
        this.id = id;
    }

    //getters
    public String getfullName(){
        return fullName;
    }

    public int getid(){
        return id;
    }
6
  • 4
    The setters must not ne static. Commented Mar 12, 2020 at 8:50
  • it is always helpful to provide the error message. Otherwise, "I keep getting an error" is not informative enough for us to help Commented Mar 12, 2020 at 8:52
  • 1
    The error I'm getting is "cannot resolve symbol 'fullName' " Commented Mar 12, 2020 at 8:54
  • new Person().setName(scan.nextLine()); use this Commented Mar 12, 2020 at 8:57
  • 1
    @Onkar Musale That I would not do, since it creates an Object that isn't assigned to a variable and therefore can't be accessed afterwards. Commented Mar 12, 2020 at 9:03

4 Answers 4

2

The error you are getting is because in class Main, you did not declare fullName. Read about variable scoping to understand more about this (intuitively, the fullName you define in the class Person is not visible from the other class Main, because it's declared as private, as it should be).

The solution is quite simple:

  1. define fullName as String in Main class,
  2. create an instance of Person, say person, and
  3. call person.setName(fullName); method (which must not be static) with the value of fullName that you read.

Here is the code for this solution:

public class Main{

    public static void main(String[] args) {

     Scanner scan = new Scanner(System.in);
     System.out.println("Enter your full name:\n");
     String fullName = scan.nextLine();
     Person person = new Person();
     person.setName(fullName);
    }
}

// Secondary class
public class Person {

    private int id;
    private String fullName;

    // Constructor
    public Person(String fullName, String id){
         this.id = id;
         this.fullName = fullName;
    }

    public Person(){             
    }

    // Setters
    public void setName(String fullName){
        this.fullName = fullName;
    }

    public void setid(int id){
        this.id = id;
    }

    //getters
    public String getfullName(){
        return fullName;
    }

    public int getid(){
        return id;
    }
}

Another option is to add a new constructor that takes only one argument (String fullName), and directly create a Person instance as Person person = new Person(fullName);.

Sign up to request clarification or add additional context in comments.

Comments

0

In order to access a Person members you have to declare a Person object in the Main class first. After you do, you will have an instance of Person and then you could use his members / methods.

Another 2 tips:

  1. The setters shouldn't be static.

  2. Your constructor does nothing. Modify it that the fullName and id variables will be assigned to the matching member:

public Person(String fullName, int id) {
    this.fullName = fullName;
    this.id = id;
}

If you don't want this behaviour, you can write an empty constructor, which doesn't receive any inputs.

8 Comments

Can you give some advice on how to modify the constructor so that variables will be assigned to matching members? I'm not sure how to go about doing that
I have added the code to my comment. If you liked my answer, please click on the 'up' arrow for me :)
Leibobitz In the case where my constructor is modified so that variables will be assigned to the matching members, do I no longer have to write this code in main? - obj.setFullName(fullName); obj.setId(id); Or can it just be done like this: Person obj = new Person(fullName, id);
Yes, exactly. You are assigning the values to each member inside the constructor. This is the correct way to do so. Please upvote my answers :)
I ran into a issue while changing it, my setters are no longer being called and it's just going straight into creating the object. Should this be an issue? Thank you, I upvoted!
|
0

1) You shall create an object of type Person. 2) Define a string to hold the name. 3) Call the function.

 String pName =  scan.nextLine();
 Person myPerson = new Person();
 myPerson.setFullName(pName);

-

public class Person {

private int id;
private String fullName;

public void setName(String fullName){
    this.fullName = fullName;
}

public void setid(int id){
    this.id = id;
} 
public String getfullName(){
    return fullName;
}
}

Also your setters shouldn't be static.

2 Comments

This cannot be compiled since his constructor receive 2 inputs.
You are right, he can use the default constructor. Edited.
0

I have made following changes:

  1. Created object of Person class in your main class and created input for id too along with declared fullName as string variable.
  2. Used set method to set the value to Person class. In this case id and fullName setters are used to set those values to person class.
  3. Replaced nextLine() method by simply next() method.
  4. toString() method is added in Person class to print Person Object values.

Refer below mentioned code:

// Main Class
public class Main{

    public static void main(String[] args) {

     Scanner scan = new Scanner(System.in);
        System.out.println("Enter your id:");
        int id = scan.nextInt();

        System.out.println("Enter your full name:");
        String fullName = scan.next();

        Person obj = new Person();
        obj.setFullName(fullName);
        obj.setId(id);

        System.out.println(obj);
    }
}
// Secondary class
public class Person {

    private int id;
    private String fullName;

    public Person() {
        super();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", fullName=" + fullName + "]";
    }

1 Comment

This cannot be compiled since his constructor receive 2 inputs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.