Search code examples
javastringbuilder

Using string builder and array to in a loop java


Im trying to use a string builder to append both the first file with its location then new line second file with its location etc. How do I do this? Whats the correct syntax? Whats wrong with my below loop?

@Override
public List<FileUpload> uploadFile(MultipartFile[] files, String windowsUsername, String number) {
List<FileUpload> uploadList = new ArrayList<FileUpload>();
        for (MultipartFile file : files) {

                FileUpload result = itsmFileService.uploadAttachment(file, number);


                uploadList.add(result);     



        }
        String supportCallID;
        supportCallID = this.getSupportCallIDForTicketNumber(number);

int i = 0;
            for (FileUpload loopLocation : uploadList){
                notesSection = uploadList.get(i).getLocation();
                StringBuilder sb = new StringBuilder();
                sb.append(notesSection);
                sb.toString();
            }

    }

}


Solution

  • You are creating a new StringBuilder everytime the loop is run. Try the following:

    StringBuilder sb = new StringBuilder();
    
    for (FileUpload loopLocation : uploadList){
         string notesSection = uploadList.get(i).getLocation();
    
         sb.append(notesSection);
    }
    
    sb.toString();