简体   繁体   English

打印数组-覆盖toString()方法

[英]Printing arrays - overriding toString() method

I have two different class, Student and StudentTest - the Student class has; 我有两个不同的班级,Student和StudentTest-学生班级有;

public class Student {

// Data Members 
private String name; // The name of this student
private long idNumber; // The ID number of this student

// Constructs a new Student with passed name and ID number parameters.
public Student(String studentName, long studentIDNumber) {
name = studentName;
idNumber = studentIDNumber;
}

// Returns the name of this student.
public String getName() {
return name;
}

// Returns the ID number of this student.
public long getIDNumber() {
return idNumber;
}

// Sets the name of this student.
public void setName(String studentName) {
name = studentName;
}

// Sets the ID number of this student.
public void setIDNumber(long studentIDNumber) {
idNumber = studentIDNumber;
}

@Override
public String toString(){
    return "Name: " + this.name;
}

} // end class

And the StudentTest class has 3 different methods, 1. to ask the user to enter the size of an array and then create an array of type Student, 2. to ask the user to populate the array with names and ID numbers, 3. to display the contents of the array. StudentTest类具有3种不同的方法:1.要求用户输入数组的大小,然后创建类型为Student的数组; 2.要求用户用名称和ID号填充数组; 3.要求显示数组的内容。

import java.util.Scanner; 

public class StudentTest { 

// Main method.
public static void main(String [] args) {

    Student[] students = createArray();
    populateArray(students);
    displayArray(students);
}

// Method that asks user for size of array.
public static Student[] createArray() {

System.out.println("Enter size of array: ");
Scanner userInputEntry = new Scanner(System.in);
int inputLength = userInputEntry.nextInt();
Student students[] = new Student[inputLength];

return students; 

}

// Method that asks user to populate array.
public static void populateArray(Student [] array) {

    for(int i = 0; i < array.length; i++) {
        array[i] = new Student();
        System.out.println("Enter student name: ");
        Scanner userInputEntry = new Scanner(System.in);
        array[i].setName(userInputEntry.next());
        System.out.println("Enter student ID number: ");
        array[i].setIDNumber(userInputEntry.nextLong());
    }

}

// Method that displays contents of array.
public static void displayArray(Student[] array) {

    for(int i = 0; i < array.length; i++){
        System.out.println(array[i].toString());
    }

}

}

When I try to run it, I get an error about the 当我尝试运行它时,出现关于

 array[i] = new Student();

in the for loop in the second method. 在第二种方法的for循环中。

How would you expect this to work ? 您希望它如何工作?

@Override
public void toString(){
    return "Name: " + this.name;
}

It should give you an compile error. 它应该给您一个编译错误。 You are trying to send a string back and the return type is void. 您正在尝试发送回一个字符串,并且返回类型为空。

Change it to 更改为

@Override
public String toString(){
    return "Name: " + this.name;
}

Change you main method to 将您的主要方法更改为

// Main method.
public static void main(String [] args) {
    Student[] students = createArray();
    populateArray(students);
    displayArray(students)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM