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);
}
You don’t strictly need to use a
StringBufferor aStringBuilder. When Java concatenates twoStrings, it internally uses something that is very similar to aStringBuffer. UsingStringBufferdirectly is slightly better, though I think you won’t notice it.As of the newline, use the
\ncharacter. You can pipe many\ncharacters you like, that will insert some empty lines in the string.For example:
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