0

I wish to return a String as follows:

    Josephine Blow (Associate Professor, Math)
         Teaching Assignments: **(for each course write on a seperate line)**
          Course No.: xxx \t Section No.: xxxx \t Course Name: xxxx \t Day and Time: dayOfWeek - timeOfweek
          If no teaching assignments print: none

Using an ArrayList variable named "teaches", which from it I'm getting all my information.

I just can't understand how should I return this kind of a String, which can be very long. Should I use some kind of String buffer? How can I add the seperate line between each two sections.

Here's my code, for make it clear:

public String toString() {
            String s ="";
            int i=1;
            if (this.teaches.isEmpty()){
                return "none";
            }

            for(Section current: this.teaches){
            s=s+"Course No"+i+" Section No:"+current.getSectionNo()+" Course Name:"+current.getRepresentedCourse().getCourseName()+" Day and Time"+current.getTimeOfDay();
            }

            return new String (this.getName()+" (Associatr Professor, "+this.department+" )"+s);

}
1
  • When this gets compiled, it will be converted to a StringBuilder (or StringBuffer, I forget) anyway, so in the end it really doesn't matter. Commented May 3, 2011 at 20:16

4 Answers 4

3

Yes, please use StringBuilder:

public String toString() 
{
    StringBuilder builder = new StringBuilder();
    if (!this.teaches.isEmpty())
    {
        for(Section current: this.teaches)
        {
            builder.append("["
                   .append("Course No ")
                   .append(i)
                   .append(" Section No: ")
                   .append(current.getSectionNo())
                   .append(" Course Name: ")
                   .append(current.getRepresentedCourse().getCourseName())
                   .append(" Day and Time ")
                   .append(current.getTimeOfDay())
                   .append("]");
        }
    }

    return builder.toString();
}

Or, better yet, have a Course class instead of a bunch of raw Strings and just print out the List<Course>, calling its toString() method for each entry in the List.

Sign up to request clarification or add additional context in comments.

1 Comment

or StringBuilder if access to it is via a single thread. And add a new line via the \n character e.g. "Hello \n World".
2

You don't strictly need to use a StringBuffer or a StringBuilder. When Java concatenates two Strings, it internally uses something that is very similar to a StringBuffer. Using StringBuffer directly is slightly better, though I think you won't notice it.

As of the newline, use the \n character. You can pipe many \n characters you like, that will insert some empty lines in the string.

For example:

s+="Course No"+i+" Section No:"+current.getSectionNo()+" CourseName:"+current.getRepresentedCourse().getCourseName()+" Day and Time"+current.getTimeOfDay()+"\n\n";

will leave an empty line between every course.

There are some special characters you can add using some codes that start with \. See: http://www.java-tips.org/java-se-tips/java.lang/character-escape-codes-in-java.html

7 Comments

How do I use it in my implementation?
Put literally \n at the end of every line, and put \n\n if you want to leave an empty line, too
this is not works good: s=s+"Course No"+i+" Section No:"+current.getSectionNo()+" Course Name:"+current.getRepresentedCourse().getCourseName()+" Day and Time"+current.getTimeOfDay()\n; What did you mean?
\n has to be INSIDE the string, like any other character. s += "some line\n"
Or use System.getProperty("line.separator") - it's platform independent.
|
2

As an alternative to StringBuffer you can use StringBuilder, recommended when you don't need synchronized access.

As for the newline, I'd recommend asking the System what is the appropriate end of line character(s), like this:

System.getProperty("line.separator");

Comments

1

Yes, you can construct your response with a StringBuffer or a StringBuilder (StringBuffer is thread safe but generally will be slightly less performant, so if you dont care about multi-threading use a StringBuilder)

for example:

StringBuilder builder = new StringBuilder();
for(Section current: this.teaches){
            builder.append("Course No").append(i).append(" Section No:").append(current.getSectionNo()).append(" Course Name:").append(current.getRepresentedCourse().getCourseName()).append(" Day and Time").append(current.getTimeOfDay());
            }

You can include newlines using "\n" although you need to be careful using newlines as they can be system dependent, so prob better using

System.getProperty("line.separator")

1 Comment

+1 Uhm, I find interesting the line.separator part, good point.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.