I want to append , to a StringBuilder if the variable a is less than the length of the Array.
I am incrementing the variable a everytime, and I am using the following code:
for(int n=0;n<fieldMap.length();n++)
{
int a=0;
JSONObject object = fieldMap.getJSONObject(n);
String type= object.getString("type");
String name= object.getString("name");
createTable.append(name +" ");
createTable.append(type);
a++;
if(!(a==(fieldMap.length()-1))) {
createTable.append(",");
}
}
But it is appending , at the end also which I don’t want. How can I solve this problem?
Thanks
Initialize
aoutside the for loop:Or just use
ninstead ofa.You can also simplify your logic slightly by writing the comma first: