如何将扫描仪的值分配给 object?

how to assign a value from scanner to a object?

我正在尝试从 Scanner 为 class object 赋值。我不想使用 Teacher a = new Teacher("Ali","SCF333",'M'); 因为它已经设置,我希望用户输入数据

这是我的 driver class:

public class TeacherDriver {
    public static void main(String[]args){


        String data;
        Teacher a = new Teacher();



        Scanner input = new Scanner(System.in);

        System.out.println("Please enter Employee Name: ");
        data=input.nextLine();
        a.setEmpName();
        System.out.println("Please enter Employee ID: ");
        data2=input.nextLine();
        a.setEmpID();
        System.out.println("Please enter Employee Gender: ");
        data3=input.nextLine();
        a.setGender();


        System.out.println(a.toString());


    }
}

这是 class:

public class Teacher {
    private String empID;
    private String empName;
    private Address address;
    private char gender; 
    private String [] Subject;

    Teacher(){

    }

    Teacher(String empName){
        this.empName=empName;
    }

    Teacher(String empName,String empID){
        this.empName=empName;
        this.empID=empID;
    }

    Teacher(String empName,String empID,char gender){
        this.empName=empName;
        this.empID=empID;
        this.gender=gender;
    }

    Teacher(String empName,String empID,char gender,Address address){

        this.empName=empName;
        this.empID=empID;
        this.gender=gender;
        this.address=address;
    }

    Teacher(String empName,String empID,char gender,Address address,String[]Subject){

        this.empName=empName;
        this.empID=empID;
        this.gender=gender;
        this.address=address;
        this.Subject=Subject;   
    }

    public void setEmpID(){
        this.empID=empID;
    }

    public void setEmpName(){
        this.empName=empName;
    }

    public void setGender(){
        this.gender=gender;
    }

    public String getEmpID(){
        return empID;
    }

    public String getEmpName(){
        return empName;
    }

    public char getGender(){
        return gender;
    }

    public Address getAddress(){
        return address;
    }

    public String[] getSubject(){
        return Subject;
    }

     public String toString()
     {
         String viewteacher = new String("Name: " + empName + "\nID: "+ empID +"\nGender: "+ gender + "\nSubjects: "+ Subject); 
          return viewteacher;
     }
}

我不太确定你到底想要什么,但是给你,

System.out.println("Please enter Employee Name: ");
data=input.nextLine();
a.setEmpName(data);
System.out.println("Please enter Employee ID: ");
data2=input.nextLine();
a.setEmpID(data2);
System.out.println("Please enter Employee Gender: ");
// Assuming that the user is entering correct data
data3=input.nextLine();
a.setGender(data3.charAt(0));

创建 setter 方法的方法是错误的,在这里您只是将实例变量 empName 设置为相同的实例变量 empName.

public void setEmpName(){
    this.empName=empName;
}

您必须将参数传递给 set 方法,然后该参数将被设置。

public void setEmpName(String empName){
    this.empName=empName;
}

TeacherDriverclass需要修改如下,

  System.out.println("Please enter Employee Name: ");
  data=input.nextLine();
  a.setEmpName(data);

获取输入后创建对象。

      a.setEmpName();
      a.setEmpID();

以上调用不会设置值,因为它没有传递任何参数来设置。修改Teacher对象创建如下

    Teacher a = new Teacher(data1,data2,data3);

在将输入存储到 data1、data2 和 data3 变量后包括上述调用。

您的设置器没有传递任何参数来设置值。以下是它们通常的样子

public void setSomething(String something) {
    this.something=something;
}

然后你这样调用它们:

MyClass myObject = new MyClass();
myObject.setSomething(scanner.nextLine());

您可以查看 javadocs 了解扫描器的工作原理