There are some assumptions to be made, since not all of the code is posted. We will assume that set.getEngCourse() properly returns an array of some Object, and that set cannot be null, and that .getEngCourse() returns at a minimum an empty array and not a null if there are no courses (if either of these assumptions can be violated, add appropriate null checks). It would be best if the returned array were of some specific type (e.g., EngCourse), but the OP code does not make clear what is in the array.
I would approach the solution in a manner similar to the following:
StringBuilder txt = new StringBuilder(); // get something to collect the output
for (Object obj : set.getEngCourse) { // If possible, change Object to the specific type
// add a line break if we have already added something,
if (txt.length() > 0) {
txt.append("\n");
}
txt.append(String.valueOf(obj)); // will handle null objects
}
taken = new TextArea(txt.toString()); // assumes taken is declared elsewhere
If there is a known object type, it would be better to:
- Override the
.toString() on the object type
Use the specific object type in the iteration
for (EngCourse ec : set.getEngCourse()) { //use specific type
...
txt.append(ec.toString()); //technically, the .toString() is not needed
}
Also, if the .getEngCourse() returns multiple objects, I would recommend changing the name to .getEngCourses() to make clear that it is returning N courses, and not just a single course.
If a TextArea is not strictly required, I would also consider using a JList or something similar. Basically, dumping everything in to a TextArea simply gives output, without much ability to do anything else with it (such as select a particular course for future operations). Consider, e.g., this SO answer Java JList model. In essence model the domain using appropriate classes, and then use a model/view/controller approach to display the domain classes rather than thinking of the domain as essentially String objects.
txtobject aStringBuilder?