1

In Java, I'm trying to concatenate the Strings from an ArrayList (called filesToReport) into a single String, as I want to display all file names in a single error message in a dialog box if they do not match certain criteria in a file opener. The solution I'm using now is a StringBuilder, and in principle it works. However, the problem is that if I eg. open three files that don't match the criteria, I first get one box listing file no. 1, then a box listing files no. 1 and 2, and then finally a box listing files no. 1, 2 and 3. The last box is the single box I want. Is there a way to achieve this?

My solution so far looks as follows:

if(filesToReport.size() > 0) {
    StringBuilder sb = new StringBuilder();

    for(String fileToReport : filesToReport) {
        sb.append(fileToReport).append(",");
    }

    String incompatibleFiles = sb.toString();
    String errorMessage = "The following files were not loaded \n" +
                          "as the are incompatible: \n" +
                          incompatibleFiles;
    JOptionPane.showMessageDialog(frame, errorMessage);
}
4
  • 4
    The behavior you describe is not the behavior of the code you have posted. Commented Jan 16, 2014 at 11:53
  • Im not sure that I understood question, but I think that if filesToReport will be HashSet and not ArrayList, you will avoid this behaviour, because set will contains only unique strings. Commented Jan 16, 2014 at 12:00
  • I agree with Marko Topolnik, your solution is working correctly, as far as I can tell... Commented Jan 16, 2014 at 12:08
  • Is there some outer loop you havent included in your post here? As that may be relevant. It looks like this code is being called every time you add something to the list Commented Jan 16, 2014 at 12:11

2 Answers 2

1

I can't see the problem in this code snipplet, but my guess would be that you are appending the error message to the same filesToReport List. So it will contain the previous error messages.

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

1 Comment

Cheers... I had inadverdently put this block inside another for-statement. Sometimes it helps just to have fresh eyes look at the code :)
1

As you were posting here, you may have corrected your error. The behavior you describe would have been caused by a misplaced brace:

if(filesToReport.size() > 0) {
    StringBuilder sb = new StringBuilder();

    for(String fileToReport : filesToReport) {
        sb.append(fileToReport).append(",");

    String incompatibleFiles = sb.toString();
    String errorMessage = "The following files were not loaded \n" +
                          "as the are incompatible: \n" +
                          incompatibleFiles;
    JOptionPane.showMessageDialog(frame, errorMessage);
    }
}

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.