0

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+";";

    }
miken32
  • 42,008
  • 16
  • 111
  • 154
Nicolas
  • 246
  • 1
  • 6
  • 23

2 Answers2

1

between okand ok2 you need to reset loop to zero again. That's all:

...
if(ok2){
    loop=0;
    int i = 3;
    for(int x=0;x<i;x++){
        String tes4 = "mau";
        if(loop>0){
            invalid2++;
            testing=testing+",";
Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
  • Woww thanks. its good and better than this condition `testing = testing.startsWith(",") ? testing.substring(1) : testing;` – Nicolas Jan 25 '14 at 12:20
0

Since you are generating the output string, you know exactly what the position of that last comma is. So you can use deleteCharAt function for StringBuilder (recommended for intensive string operations). This will be the fastest approach as against you comparing each character, etc.

Also check out why you should use StringBuilder https://stackoverflow.com/a/5234160/415412

Community
  • 1
  • 1
Suchintya
  • 605
  • 5
  • 15