使用扫描仪添加到空对象

Using Scanner to add to empty Object

我正在尝试将用户输入添加到一个空对象,但结果为空。我想我正确地设置了扫描仪,但我不确定。我不确定应该如何设置扫描仪以链接到空对象。我在另一个 class.

上设置了 setter 和 getter
 import java.util.Scanner;

 public class StudentRecordTestHarness
 {
      public static void main (String []args)
      {
           StudentRecord sr1 = new StudentRecord ("Tim", "Lost", 12345, 75);

           System.out.println("First Name: " + sr1.getFirstName());
           System.out.println("Last Name: " + sr1.getLastName());
           System.out.println("Student ID: " + sr1.getStudentID());
           System.out.println("Number Grade for Course: " + sr1.getGradeCourseOne());
           System.out.println("Grade Converted to Letter: " + 
                     sr1.GradeLetter(+sr1.getGradeCourseOne()));

           StudentRecord sr2 = new StudentRecord();
           Scanner userInput = new Scanner (System.in);
           System.out.println("Please Enter Student's First Name: ");
           String setFirstName = userInput.nextLine();

           System.out.println("New Student First Name: " + sr2.getFirstName());
      }
 }

您没有设置 sr2.firstName;您只是在创建一个新的局部变量 setFirstName,然后它包含您的输入。

下面设置学生记录的名字:

String setFirstName = userInput.nextLine();
sr2.setFirstName(setFirstName);