7

I am using StringBuilder to show comma between records, but also getting comma at the end of Last Record

How to control on this ? I don't like to see comma at the end of last record - because it's useless

   for (int i = 0; i < arrayList.size(); i++) {

        String name = arrayList.get(i).getName();
        int price = arrayList.get(i).getPrice();

        stringBuilder.append(strName);
        stringBuilder.append(" - "+price+",");

    }

        stringFinal = stringBuilder.toString();
Zaid Malhis
  • 588
  • 4
  • 18
Sun
  • 6,768
  • 25
  • 76
  • 131

7 Answers7

10

Java 8 Solution

You don't need any check when using Java 8. All you need is to use StringJoiner (or String.join()).

StringJoiner joiner = new StringJoiner(",");
for (int i = 0; i < arrayList.size(); i++) {
    String name = arrayList.get(i).getName();
    int price = arrayList.get(i).getPrice();

    joiner.add(strName + " - " + price);
}
String joinedString = joiner.toString();

And you can make it even cleaner by taking advantage of the Stream API (thanks to @Sasha):

String joinedString = String.join(",", list.stream().map(
        e -> e.getName() + " - " + e.getPrice()).collect(Collectors.toList()));
Fred Porciúncula
  • 8,533
  • 3
  • 40
  • 57
4

Add a check for the last item.

if (i < arrayList.size() - 1) {
  stringBuilder.append(" - "+price+",");
} else {
  stringBuilder.append(" - "+price);
}

or:

stringBuilder.append(" - "+price);
if (i < arrayList.size() - 1) {
  stringBuilder.append(",");
}
Thomas R.
  • 7,988
  • 3
  • 30
  • 39
2

You can just delete the last character:

 stringBuilder.deleteCharAt(stringBuilder.length()-1);
 stringFinal = stringBuilder.toString();

that would be the simplest solution.

Tawcharowsky
  • 615
  • 4
  • 18
1
int index = stringBuilder.lastIndexOf(",");
if (index != -1){
    stringBuilder.replace(index, index+1, "");
}
String stringFinal = stringBuilder.toString();
Casper
  • 216
  • 2
  • 7
0

Or loop till size - 1 and then add the last element after wards without a comma.

int i = 0, price = 0;
String name = null;
for (i = 0; i < arrayList.size() - 1; i++) {
 name = arrayList.get(i).getName();
 price = arrayList.get(i).getPrice();

 stringBuilder.append(strName);
 stringBuilder.append(" - "+price+",");
}

name = arrayList.get(i).getName();
price = arrayList.get(i).getPrice();
stringBuilder.append(strName);
stringBuilder.append(" - "+price);
stringFinal = stringBuilder.toString();
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
0
    for (int i = 0; i < arrayList.size(); i++) {

    String name = arrayList.get(i).getName();
    int price = arrayList.get(i).getPrice();

    stringBuilder.append(strName);
    if(i== arrayList.size()-1)
       stringBuilder.append(" - "+price);
    else
    stringBuilder.append(" - "+price+",");

     }

    stringFinal = stringBuilder.toString();
Ashraf
  • 3,114
  • 3
  • 23
  • 22
0

Instead of placing it in last you can write it in front like :-

        String name = arrayList.get(0).getName();
        int price = arrayList.get(0).getPrice();

        stringBuilder.append(strName).append(" - "+price);
for (int i = 1; i < arrayList.size(); i++) {

        name = arrayList.get(i).getName();
        price = arrayList.get(i).getPrice();

        stringBuilder.append(","+strName).append(" - "+price);

    }

        stringFinal = stringBuilder.toString();
Goyal Vicky
  • 1,249
  • 16
  • 16