0

I am trying to convert values of a 2D ArrayList to a string to that I can print them onto a JTextArea. However, everytime I run my program, the 2D ArrayList is still in the square brackets. Does anyone know a fix for this?

private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {

    for (int row = 0; row <= count; row++) {

        employeeDisplay.setText(String.valueOf(employeeRecords.get(row)));

    }
}
2
  • 1
    When you say 2D ArrayList, do you have ArrayList< ArrayList<String>>? What does employeeRecords.get(row) return? Commented May 16, 2016 at 21:25
  • @Makketronix Yeah I have that setup, and the employeeRecords.get(row) is supposed to print each row of the 2D ArrayList into the JTextArea Commented May 16, 2016 at 21:32

2 Answers 2

1

Try this in your for loop :

StringBuilder builder = new StringBuilder();
for (String value : employeeRecords.get(row)) {
    builder.append(value);
}
String text = builder.toString();
employeeDisplay.setText(text);

OR

String formatedString = employeeRecords.get(row).toString()
    .replace(",", "")  //remove the commas
    .replace("[", "")  //remove the right bracket
    .replace("]", "")  //remove the left bracket
    .trim();  
employeeDisplay.setText(formatedString);
Sign up to request clarification or add additional context in comments.

2 Comments

The top one works for me, but is there any way to make a break to a new line after each row of the 2D array is printed? As of right now it is just printing all elements on the one line.
Add builder.append("\n"); after builder.append(value); hope it helps. Also, could you please accept the answer or upvote it. It helps other people to know that an answer has been found
1

If you're using , you could use Collectors#joining

employeeDisplay.setText(employeeRecords.get(row)
                                       .stream()
                                       .collect(Collectors.joining(" "));

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.