Unknown user
Unknown user

Reputation: 45301

String manipulation in Java

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);

}

Upvotes: 0

Views: 425

Answers (4)

gd1
gd1

Reputation: 11403

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

Upvotes: 2

duffymo
duffymo

Reputation: 308763

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.

Upvotes: 3

Sarah Haskins
Sarah Haskins

Reputation: 1385

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");

Upvotes: 2

rhinds
rhinds

Reputation: 10043

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")

Upvotes: 1

Related Questions