在Java中使用方法和数组时出现“无法找到符号”错误

时间:2015-11-17 02:30:15

标签: java arrays methods compiler-errors

我有一个名为Student的类,其数据成员为:

private String name;
private long idNumber;

使用正确的getter和setter方法。

我需要创建一个名为StudentTest的测试人员类,其中包括以下方法:

public static Student[] createArray()

public static void populateArray(Student[] array)

public static void displayArray(Student[] array)

当我尝试编译测试人员类时,我收到“找不到符号”错误。

这是我的测试人员类:

// Scanner class from the Java API
import java.util.Scanner;

public class StudentTester {
    public static void main(String[] args) {
        // Call methods
        Student[] studentList = createArray();
        populateArray(studentList);
        displayArray(studentList);

    } // end main

    // Create Array Method
    public static Student[] createArray() {
        // Declaration & Creation of Scanner object
        Scanner input = new Scanner(System.in);

        // Create Array
        System.out.println("Enter size of array: ");
        int i = input.nextInt();
        Student[] studentList = new Student[i];
        return studentList;
    }       

    // Populate Array Method 
    public static void populateArray(Student[] array) {
        for(int i = 1; i < studentList.length; i++) {
            studentList[i] = new Student();
            Scanner userInput = new Scanner(System.in);
            // Get Input for Name 
            System.out.println("Enter Student Name: ");
            studentList[i].setName = userInput.nextLine();
            // Get Input for Id Number
            System.out.println("Enter Student ID Number: ");
            studentList[i].setIdNumber = userInput.nextInt();
        }
    }

    // Display Array Method
    public static void displayArray(Student[] array) {
        System.out.println("Array Contents");
        for(int j = 0; j < studentList.length; j++) {
            System.out.println(studentList[j].getName() + " " + studentList[j].getIdNumber());
        }
    }

} // end class

请任何人帮助我,我对java很新。

我的Student班级

public class Student {

    // Data Members
    private String name;
    private int idNumber;

    // Constructor
    public Student() {
        name = "Unassigned";
        idNumber = 0;
    }

    // Getters
    public String getName(){
        return name;
    }   

    public int getIdNumber(){
        return idNumber;
    }

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

    public void setIdNumber(int idNumber){
        this.idNumber = idNumber;
    }


} // end class

无法找到的符号与数组本身studentList

有关

1 个答案:

答案 0 :(得分:0)

populateArray(和displayArray)正在尝试使用studentList,但您在参数中将其命名为array。重命名,如

public static void populateArray(Student[] studentList) { // <-- not array.

public static void displayArray(Student[] studentList) { // <-- and here.

在您的方法中使用array代替studentList