简体   繁体   中英

Using scanner to assign value to a variable in another class?

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;
    }

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); .

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.

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 + "]";
    }

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.

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