【问题标题】:Passing console input from scanner into a java object, cannot find symbol error将扫描仪的控制台输入传递给 java 对象,找不到符号错误
【发布时间】:2021-05-09 03:21:48
【问题描述】:

我有一个学生类,它由名字和姓氏方法和构造函数组成。我需要创建一个传递控制台输入并将其分配给学生对象的方法,并且可以在 main 方法中分别打印全名和名字。

学生班

public class Student {
    private String firstName;
    private String lastName;

    public Student(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
    }

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

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
            return firstName;
    }

    public String getLastName() {
            return lastName;
    }

    @Override
    public String toString(){
        return firstName + " " + lastName;
    }
}

学生姓名班级

import java.util.Scanner;

public class StudentName {
    public static void main(String[] args) {
        System.out.println("Your full name is " + getStudentFullName(newStudent));
        System.out.println("Your first name is " + getStudentFullName(firstName));  
    }
    
    public static void getStudentFullName(Student student){
        Scanner sc = new Scanner(System.in);
        System.out.print("What is your full name?");
        String name = sc.nextLine();
        name = name.trim();
        int index1 = name.indexOf(" ");
        String firstName = name.substring(0,index1);
        String lastName = name.substring(index1 + 1);
        Student newStudent = new Student(firstName,lastName);
    }  
}

更新:错误消息

error: cannot find symbol
System.out.println("Your full name is " + getStudentFullName(newStudent));
symbol:   variable newStudent
location: class StudentName

error: cannot find symbol
System.out.println("Your first name is " + getStudentFullName(firstName));  
symbol:   variable firstName
location: class StudentName

【问题讨论】:

  • complete 错误信息是什么?请在您的问题中包含所有相关详细信息。
  • @StephenC 添加错误。
  • OK ... 那么为什么在getStudentFullName 中声明newStudent?在你使用它的方法中声明它。同样,你为什么在main 中使用firstName?它没有在main 中声明。
  • 另外...查看getStudentFullName 并了解其用途。然后问自己,为什么要打印getStudentFullName 的>>结果
  • 我建议您阅读Rubber Duck Debugging。这是一种涉及阅读您自己的代码并理解它的技术……通过尝试向(想象中的)其他人解释它。

标签: java class methods


【解决方案1】:

您以错误的方式调用 getStudentFullName 方法。 在System.out.println("Your full name is " + getStudentFullName(newStudent)); 行中,newStudent 变量在哪里?您可以有两种方法:首先您可以在 main 方法中定义它并将其传递给 getStudentFullName 方法,并在此方法中更改传递的参数,而不是创建一个新参数,如下所示:

public class StudentName {
public static void main(String[] args) {
    Student newStudent = new Student();
    System.out.println("Your full name is " + getStudentFullName(newStudent));
     
}

public static void getStudentFullName(Student student){
    Scanner sc = new Scanner(System.in);
    System.out.print("What is your full name?");
    String name = sc.nextLine();
    name = name.trim();
    int index1 = name.indexOf(" ");
    String firstName = name.substring(0,index1);
    String lastName = name.substring(index1 + 1);
    student.setFirstName(firstName);
    student.setLastName(lastName);
}  
}

或者您可以从getStudentFullName 方法返回newStudent,如下所示:

public class StudentName {
public static void main(String[] args) {
    System.out.println("Your full name is " + getStudentFullName(newStudent));
     
}

public static Student getStudentFullName(Student student){
    Scanner sc = new Scanner(System.in);
    System.out.print("What is your full name?");
    String name = sc.nextLine();
    name = name.trim();
    int index1 = name.indexOf(" ");
    String firstName = name.substring(0,index1);
    String lastName = name.substring(index1 + 1);
    Student newStudent = new Student(firstName,lastName);
    return newStudent;
}  
}

但是在System.out.println("Your first name is " + getStudentFullName(firstName)); 行中,firstName 是什么?如果它是字符串变量,则不能将其传递给getStudentFullName,因为它接受 Student 类型的变量作为参数而不是字符串。

毕竟获取输入表单控制台的代码最好放在main方法中!

【讨论】:

    猜你喜欢
    • 2015-06-02
    • 1970-01-01
    • 2016-05-19
    • 2015-11-27
    • 2023-03-20
    • 2014-06-21
    • 2018-10-30
    • 2017-07-12
    • 1970-01-01
    相关资源
    最近更新 更多