Domanda

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);
}
È stato utile?

Soluzione

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.

Altri suggerimenti

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);
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top