How to separate my output by comma in java

I made this program to calculate set of numbers

public void himpunanIrisan(){
     System.out.print("The Result is  {");
     for(int k=1; k<=setofB; k++){
         boolean Sama = false;
         for(int i=1; i<=setofA; i++){
             if(numberB[k] == numberA[i]){
                 Sama = true;
             }
         } if (Sama==true){
             System.out.print(numberB[k]+ ",");
     }
     } System.out.println("}");
 }

and the output will show like this

The Result is {1,2,}

is there any way so the other comma will dissepear?

Answer

I have assumed that you are just a beginner and trying to learn that’s why I try to use basic level solution.

    public void himpunanIrisan(){
        int[] numberB = {0,1,2,3,4};
        int setofB = numberB.length-1;
        int[] numberA = {0,1,2,5,6};
        int setofA = numberA.length-1;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("The Result is  {");
        boolean initParam = true;
        for(int k=1; k<=setofB; k++){
            boolean Sama = false;
            for(int i=1; i<=setofA; i++){
                if(numberB[k] == numberA[i]){
                    Sama = true;
                }
            } if (Sama && initParam){
                initParam = false;
                stringBuilder.append(numberB[k]);
            } else if(Sama) {
                stringBuilder.append(",");
                stringBuilder.append(numberB[k]);
            }
        }
        stringBuilder.append("}");
        System.out.println(stringBuilder);
    }
}

The Result is {1,2}