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);
5
  • 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? Commented Mar 1, 2015 at 7: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. Commented Mar 1, 2015 at 7:31
  • @BaseZen Thank you for your comment but I can't understand what you want to say? Kindly tell me in easy understandable version. Commented Mar 1, 2015 at 7: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 Commented Mar 1, 2015 at 7:33
  • As explained clearly in this question, 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. Commented Mar 1, 2015 at 7:37

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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);  
    }

}

2 Comments

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.
@Sanketh Katta Thank U I understood what I missed.
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.