我在JAVA中读取数据后打印数组时遇到问题

时间:2012-12-29 14:50:08

标签: java printing

我在阅读后打印数组有问题。打印后,打印内存地址,而不是数组的值。我能做些什么呢?

public class MyClass
{
     Student St = new Student();

     Student[]Array1 = new Student[10];


     void AddList()
     {

            Scanner Scan = new Scanner(System.in);

            for (int i=0; i<Array1.length & i<ArrayF1.length; i++)
            {
                System.out.println("Enter Student NAME Number " + (i+1) + ":");
                Array1[i] = new Student();
                Array1[i].setName(Scan.next());


                //System.out.println("Enter Student MARK Number " + (i+1) + ":");
                //St.setMark(Scan.nextFloat());
            }

    }

这是我的打印方法。打印的结果就像这样

(studentproject.Student@1a758cb)

 void PrintList()
 {

            for (int i=0; i<Array1.length; i++)
            {

                System.out.println(Array1[i]);

            }
 }

这是我的学生班,我有我所有的setter和getter方法...所以我有3个Class我如何使用这个3类,其中一个获取数据,另一个打印Mark数据并在第三类打印学生姓名数据......我怎么能这样做...我做了一些代码,但我不知道它是否正确...感谢您的帮助......

public class Student
{
private String Name;
private float Mark;

/**
 * @return the Name
 */
public String getName() {
    return Name;
}

/**
 * @param Name the Name to set
 */
public void setName(String Name) {
    this.Name = Name;
}

/**
 * @return the Mark
 */
public float getMark() {
    return Mark;
}

/**
 * @param Mark the Mark to set
 */
public void setMark(float Mark) {
    this.Mark = Mark;
}

}

2 个答案:

答案 0 :(得分:3)

只需覆盖toString()类中的Student方法,并在打印实例时返回要打印的相应字符串。

它可能看起来像: -

@Override
public String toString() {
    return "Name: " + studentName;
}

目前,调用了toString()类的Object方法的默认实现,您看到的是从该方法返回的格式,其格式为 - Type@hashCode < / p>

答案 1 :(得分:0)

这里我添加了一些可以覆盖toString()方法的内容

public class Student {

    private String name;
    private int id;
    float mark;

    public Student() {
    }

    public Student(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public float getMark() {
        return mark;
    }

    public void setMark(float mark) {
        this.mark = mark;
    }

    @Override
    public String toString() {
        return "Student[ID:" + id + ",Name:" + name + ",Mark:"+mark+"]";
    }

    public void printStudentInfo() {
        // print all the details of student
    }
    public static void main(String[] args) {
        Student[] students = new Student[10];
        Scanner scanner = new Scanner(System.in);

        for (int i = 0; i < students.length; i++) {
            System.out.println("Enter Student Name " + (i + 1) + ":");
            String name = scanner.nextLine();
            Student student = new Student(name, i + 1);
            System.out.println("Enter Student MARK Number " + (i + 1) + ":");
            float mark = scanner.nextFloat();
            student.setMark(mark);
            students[i]=student;
        }
        for(Student student:students) {
            // by default toStirng method is called
            System.out.println(student);
            //or you can call like
            //student.printStudentInfo();
        }
    }
}