0

I have an array of objects and i want to print them out to a TextArea in a JavaFX program. Im not sure how to go about doing that. Everything that im try doesnt work.

for(int i = 0; i < set.getEngCourse().length; i++){
 txt.append(set.getEngCourse()[i]);
 if(i != set.getEngCourse().length -1){
   txt.append("\n");
 }

taken = new TextArea(txt.toString());

The object im trying to get onto the TextArea is a couple of course objects.

5
  • Is the txt object a StringBuilder? Commented May 8, 2016 at 15:16
  • Just a string. should it be? Commented May 8, 2016 at 15:17
  • 1
    Your code is missing a } Commented May 8, 2016 at 15:21
  • StringBuilder isnt working with an object array. @Loris Securo I know it is the code editor removed it on here for some reason. Commented May 8, 2016 at 15:21
  • you can edit your question and add it Commented May 8, 2016 at 15:24

2 Answers 2

1

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.

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

1 Comment

That wasnt it, but very close. Gave me a good jumping off point. I got it working thanks :)
0

Is your txt object a string? If so then why are you putting a txt.toString in the last line of your snippet?

You can use setText function to display the contents in your text area.

Post the error or problem more specifically and extend your code snippet for us to know the scope of for loop.

3 Comments

I cant just use the setText. Im trying to get an array of objects onto the textArea. The loop is what i was using to read the array and try to print it to the text area
What is the type of txt? And what error are you getting?
NPE at the txt.append(set.getEngCourse()[i]); and i changed it to a StringBuilder

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.