Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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);
}
share|improve this question
4  
The behavior you describe is not the behavior of the code you have posted. –  Marko Topolnik yesterday
 
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. –  Divers yesterday
 
I agree with Marko Topolnik, your solution is working correctly, as far as I can tell... –  Enerccio yesterday
 
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 –  cowls yesterday
add comment

2 Answers

up vote 1 down vote accepted

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);
    }
}
share|improve this answer
add comment

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.

share|improve this answer
 
Cheers... I had inadverdently put this block inside another for-statement. Sometimes it helps just to have fresh eyes look at the code :) –  SorenRomer yesterday
 
Upvote and/or Accept? –  Gábor Csikós yesterday
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.