0

I'm currently having an issue where when I output my array into an ordered list, it only outputs the very last song in the collection of five. Any help on how I can make it display all five would be appreciated. (First time trying to use arrays).

private void initActionPerformed(java.awt.event.ActionEvent evt) {
    ArrayList songTitle = new ArrayList();
    Collections.addAll(songTitle, "Pink Floyd - The Dark Side of the Moon", "AC/DC - Back in Black", "Led Zeppelin - Led Zeppelin IV", "Billy Joel - Piano Man", "Eric Clapton - Unplugged");
    Collections.sort(songTitle);
    for (int j = 0; j < songTitle.size(); j++) {
        nameOutput.setText(j + "- " + songTitle.get(j));
    }
}

3 Answers 3

3

You are overwriting using setText here

for (int j = 0; j < songTitle.size(); j++) {
    nameOutput.setText(j + "- " + songTitle.get(j));
}

append string first and then set to nameOutput.

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

Comments

0

I believe you expect something like this. ?

StringBuilder sb = new StringBuilder();
sb.append("");
for (int j = 0; j < songTitle.size(); j++) {
  sb.append(j);
  sb.append("- ");
  sb.append(songTitle.get(j));
  sb.append(" ");
}
nameOutput.setText(sb.toString());

1 Comment

I believe that was what I was expecting. Thankyou for the help, it is greatly appreciated!
0

You're using arrays properly, but you seem to be overwriting the contents of nameOutput each time you go through the loop.

Comments

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.