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);
}