문제

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);
}
도움이 되었습니까?

해결책

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.

다른 팁

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);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top