This just was my curiousity about adding comma in looping between getItem. i want my item would be like this item1,item2. but its instead do it either like this ",item1,item2" or "item2,item2,". This is what my code looks like :
public static void main(String args[]) throws Exception
{
msgAll = "Tsting messge";
boolean ok = true;
boolean ok2 = true;
int invalid = 0;
int invalid2=0;
int loop =0;
String tes ="",testing="";
//========================================================//
if(ok){
int y = 2;
for(int j=0;j<y;j++){
String tes2 = "aku";
if(loop>0){
invalid++;
tes=tes+",";
}
tes=tes+tes2;
loop++;
}
if(invalid>0) {
msgAll=msgAll+" item : " + tes+";";
}
}
//========================================================//
if(ok2){
int i = 3;
for(int x=0;x<i;x++){
String tes4 = "mau";
if(loop>0){
invalid2++;
testing=testing;
}
testing=testing+tes4+",";
loop++;
}
if(invalid2>0) {
msgAll=msgAll+" region : " + testing+";";
}
}
System.out.println(msgAll);
}
The printout is like this :
Tsting messge item : aku,aku; region : mau,mau,mau,;
But if i do it like this : (the ok2 code i replace)
if(ok2){
int i = 3;
for(int x=0;x<i;x++){
String tes4 = "mau";
if(loop>0){
invalid2++;
testing=testing+",";
}
testing=testing+tes4;
loop++;
}
if(invalid2>0) {
msgAll=msgAll+" region : " + testing+";";
}
}
The printout would be like this in ok2 :
Tsting messge item : aku,aku; region : ,mau,mau,mau;
the point is i want my message perfect like this : in ok = aku,aku and in ok2 = mau,mau,mau
Sorry for my unknowledgement
SOLVED : i just place this
testing = testing.startsWith(",") ? testing.substring(1) : testing;
so the code would be like this
if(invalid2>0) {
testing = testing.startsWith(",") ? testing.substring(1) : testing;
msgAll=msgAll+" region : " + testing+";";
}