private String generateSuffix(List<String[]> searches, List<String[]> sorts) {
String ret = "";
if (searches != null && !searches.isEmpty()) {
ret += "where ";
for (String[] search : searches) {
if (search.length < 3) {
continue;
}
ret += search[0] + search[1] + search[2] + " and ";
}
ret = ret.substring(0, ret.length() - 5);
}
ret += " order by ";
if (sorts != null && !sorts.isEmpty()) {
for (String[] sort : sorts) {
if (sort.length < 2) {
continue;
}
ret += sort[0] + " " + sort[1] + ",";
}
}
return ret;
}
}
My SONAR scan indicated that using '+' to concatenate strings in a loop is a major error. It does not give much insight as to how this error can be fixed. Can StringBuilder be used in this case? If so, how would I go about implementing the StringBuilder with the enhanced for loop? I'm not very familiar with using StringBuilder.
Also, can I add a StringBuilder within a list as shown below? I need to get rid of the code "value += ds;" and value = d + "" ".
private List<TransactionSearchField> checkSearches(List<TransactionSearchField> searchesIn) {
List<TransactionSearchField> searches = new ArrayList<TransactionSearchField>();
if (searchesIn != null) {
for (TransactionSearchField search : searchesIn) {
String value = search.getValue();
if (value != null && value.length() > 0) {
if (search.getDataType().equals(double.class)) {
String[] dSplit = value.split(",");
value = "";
for (String ds : dSplit) {
value += ds;
}
double d;
try {
d = Double.parseDouble(value);
value = d + "";
} catch (IllegalArgumentException e) {
value = "";
}
} else if (search.getDataType().equals(Date.class)) {
value = formatDate(generateDate(value));
}
searches.add(new TransactionSearchField(search.getDataType(), search.getTableField(), search.getLogic(), value, search.getViewField()));
}
}
}
return searches;
}
StringJoinermay be an option. @Rainbolt: Not a duplicate of that question. The OP only removes the last" and ". Also it's not a question about the performance.