0

In my application, a user will select multiple options out of 10 options. Numbers of selected options may vary from 1 to 10. Now I am trying to separate these selected options by inserting a comma in between the numbers. I am getting the numbers like this:

 123456
 346
 12
 5

Now I am trying to convert them like this:

 1,2,3,4,5
 3,4,6
 1,2
 5(no comma)

For this I am trying StringBuffer, but I'm getting the wrong output:

 For 12 output is 1,2
 For 5 output is 5
 For 123 output is 1,,23
 For 123456 output is 1,,,,,23456

Can you help me find the mistake in my code?

String str = jTextField1.getText();
StringBuffer sb = new StringBuffer(str);
int x = 0;    
for (int i = 0; i < str.length() - 1; i++) {    
   sb.insert(++x, ",");
}
System.out.println(sb);
mjuarez
  • 16,372
  • 11
  • 56
  • 73
Vishal Deb
  • 43
  • 4
  • 1
    I feel like pedagogically, this should be hinted at, but that the questioner should solve. Think what happens as the StringBuffer grows in length with that comma: what does that mean about the index of everything after it? – BaseZen Mar 01 '15 at 07:28
  • Is using `StringBuffer` necessary? It seems like you are using it locally within a method, thus a `StringBuilder` might be more fit for the job. – initramfs Mar 01 '15 at 07:31
  • @BaseZen Thank you for your comment but I can't understand what you want to say? Kindly tell me in easy understandable version. – Vishal Deb Mar 01 '15 at 07:31
  • @CPUTerminator Thank you I am using these classes first time as learner I will be thankful if you can put more lights on your comment – Vishal Deb Mar 01 '15 at 07:33
  • As explained clearly in [this question](http://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer), a `StringBuffer` is useful when multiple threads are writing to the object (which is almost never the case) whilst a `StringBuilder` is the same object without synchronization overhead, allowing for better performance. – initramfs Mar 01 '15 at 07:37

3 Answers3

1

You can use regex to achieve it with very simple and fast code:

    String num = "123456789";
    String regex = "(\\d)(?=(\\d{1})+$)";
    String commaSaperatedNums = num.replaceAll(regex, "$1,");
    System.out.println( commaSaperatedNums);
Abhijeet Dhumal
  • 1,799
  • 13
  • 24
0

@SMA's answer works, but another solution using your code as a base is as follows:

Your StringBuffer is growing as you are inserting commas, which makes your x index out of sync with the actual length of the buffer, therefore you'll need to increment it another time after inserting the comma.

class Test {

  public static void main(String[] args) {
    String str="123456";
    StringBuffer sb=new StringBuffer(str);
    int x=0;
    for (int i = 0;i < str.length()-1; i++)
      {
        sb.insert(++x, ",");
        x += 1;
      }
      System.out.println(sb);  
    }

}
Sanketh Katta
  • 5,961
  • 2
  • 29
  • 30
  • Since people have handed a beginner the answer rather than letting him find it himself (oh well, that's how this site works), I challenge @Vishal Deb to rewrite the loop by walking *backwards* through the `StringBuffer` and observing how the indexing logic changes. – BaseZen Mar 01 '15 at 07:40
  • @Sanketh Katta Thank U I understood what I missed. – Vishal Deb Mar 01 '15 at 07:43
0

Actually whats happening is, your x=0 initially, but then you insert at '++x' so it goes in 'x-1'. But in next iteration you again do '++x', and it stores comma in 'x=2' but it should be storing in 'x=3' actually, as the size of your array has increased.

Try this.

String str=jTextField1.getText();
StringBuffer sb=new StringBuffer(str);
int x=-1;    
for (int i = 0;i < str.length()-1; i++)
{    
x+=2
sb.insert(x, ",");
}
System.out.println(sb);
rohanpro
  • 132
  • 7