2

I have a class containing some basic info about a course and an ArrayList containing objects of student class.
Is there a smarter way of editing the toString() method in order to print out information about the course and each student (on a separate line each) then this:

public String toString() {
    System.out.println("Course " + this.name + ", Teacher: " + this.teacher;
    for (Student stud: studentList) {
        System.out.println(stud);
    }
    retutn "";

I have already set the Student class' toString() method to print Student info.

*edit: Realised I made a mistake with print(stud), so changet to println(stud)

1
  • 3
    System.out.println(stud); println Commented Feb 18, 2015 at 15:31

4 Answers 4

5
  • If you want to print one student per line, use System.out.println instead of System.out.print
  • You shouldn't print stuff in toString, you should just return a descriptive string.

In this case it looks like you wanted a method to print stuff, so use something else than toString. Perhaps printCourseInfo for instance:

public void printCourseInfo() {
    System.out.println("Course " + name + ", Teacher: " + teacher);
    for (Student stud: studentList) {
        System.out.println(stud);        // Note println instead of print
    }
}

Bonus part: If you're using Java 8, you could do

studentList.forEach(System.out::println);
Sign up to request clarification or add additional context in comments.

2 Comments

I assumed that printing in toString should be avoided. What's stopping me from actually defining a separate print method is the instruction I was given for this homework, which asks me to modify toString in order "to provide a printout of the course information first, followed by the information of all the students contained on the array".
You should then 1) use the toString as method name, 2) create a String result in the beginning of the method, and 3) change each printout to result = result + <string to append> and 4) end the method with return result
1

You should use System.out.println(stud) in stead of System.out.print(stud)

What the "println" does, is printing each Student on a new line

Also see this for everything on System

Comments

0

When you have a toString() method you should be returning a string, not printing it out.

Since you're using System.out.print in this case, that means you probably want to print stuff out. So change it to System.out.println in your for-each loop

public void printCourseInfo() { 
    System.out.println("Course " + name + ", Teacher: " + teacher);
    for (Student stud: studentList) {
        System.out.println(stud);    
    } 
} 

Comments

0

Others have mentioned that you shouldn't really repurpose toString(). Instead you should implement a method like 'getAsPrettyString()' etc. If you're lazy (like me) you might want to use something like this :

import java.util.List;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

public class Blah {
    private String hello;
    private List<String>helloList;

    public String getAsPrettyString() {
        return ReflectionToStringBuilder.toString(this);
    }   
}

Using commons lang etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.