-2

I am trying to use toString() method , but somehow doesn't work, instead of getting the actual values ( name and numbers of student) I am getting this :

================= students list =====================

----------------------
com.se.classes.Student@4554617c
com.se.classes.Student@74a14482
com.se.classes.Student@1540e19d
===============================================

This is the method in the com.se.classes :

public String toString() {
    s = "\n================= students list =====================\n";
    s = s + "\n----------------------";
    for (int i = 0; i < nb; i++) 
    s = s + "\n" + student[i];
    s = s + "\n===============================================";
    return s;
}

Please help. Thanks.

Andreas
  • 21,535
  • 7
  • 47
  • 56
  • 8
    `javascript !== java`! – Nina Scholz Mar 11 '20 at 17:49
  • What's the expected output anyway? And what's this `Student` class? – ionizer Mar 11 '20 at 17:50
  • 4
    Does this answer your question? [How do I print my Java object without getting "SomeType@2f92e0f4"?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Tim Hunter Mar 11 '20 at 17:55
  • You see the default implementation of the Student class. This includes the class name and the internal address of the object. You can override the toString method in Student class if you want a different behavior. – Stefan Mar 11 '20 at 18:00

3 Answers3

0

You need to add a toString() function to your Student class. I would do this for you but you have not posted the Student class. Feel free to and I will update with an example.

Here is an assumed Student class.

    static class Classroom {

        private static final String HEADER = "================= students list =====================";

        private static final String FOOTER = "=====================================================";

        private final Student[] students;

        Classroom(Student[] students) {
            this.students = students;
        }

        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder(HEADER).append("\n");

            for (Student student : students) {
                builder.append(student).append("\n");
            }
            return builder.append(FOOTER).toString();
        }

    }

    static class Student {

        private final String name;

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

        @Override
        public String toString() {
            return String.format("name=%s", name);
        }
    }

Test

    public static void main(String[] args) {
        Student[] students = new Student[] {
                new Student("Foo"),
                new Student("Bar")
        };

        Classroom classroom = new Classroom(students);

        System.out.println(classroom);
    }

Output

================= students list =====================
name=Foo
name=Bar
=====================================================
Jason
  • 5,154
  • 2
  • 12
  • 22
0

This example in case you do not want (or can't) to add toString() method to Student class directly:

class Student {
    private final String firstName;  // John
    private final String lastName;   // Doe

    // getters
}

static String toString(String[] students) {
    StringBuilder buf = new StringBuilder();
    buf.append("================= students list =====================\n");

    for(Student student : students) {
        buf.append(student.getFirstName()).append(' ').append(studnet.getLastName);  // John Doe
        buf.append("\n----------------------");     
    }   

    return buf.tpString();
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
0

Your Student class needs a toString() too. Also your toString is inefficient:

public String toString() {
    StringBuilder s = new StringBuilder(System.lineSeparator())
                          .append("================= students list =====================")
                          .append(System.lineSeparator())
                          .append(System.lineSeparator())
                          .append("----------------------";
    for (int i = 0; i < nb; i++) 
        s.append(System.lineSeparator())
         .append(student[i]);

    s.append(System.lineSeparator())
     .append("===============================================");

   return s.toString();
}
Nexevis
  • 4,647
  • 3
  • 13
  • 22
Ryan
  • 1,762
  • 6
  • 11