在逗号中将逗号附加到java中

时间:2013-06-28 11:33:52

标签: java

我有一个字符数组。我想在每个第3个字符后添加逗号(,)。我尝试了以下代码。

    public class Comma {
    char [] str = {'1','2','3','4','5','6','7','8','9'};
    char [] buf = new char[15];
    int size = str.length;
    int c=1;
    public void insert()
    {
            for(int i=0;i<size;i++)
            {
                  c++;
                  if(c%3==0)
                  {
                    buf[c] = ',';
                    i++;
                  }
                  buf[i]=str[i];
             }  
            System.out.println("Final String");
            for(int i=0;i<buf.length;i++)
            System.out.print(buf[i]);
   }
   public static void main(String args[])
   {
        Comma c = new Comma();
        c.insert();
   }

   }

我得到以下输出:

Final String
1 345 789

有人可以纠正我吗?

5 个答案:

答案 0 :(得分:0)

这个会生成123,456,789

public class MySimpleClass {

    public static void main(String[] args) {        
        char [] str = {'1','2','3','4','5','6','7','8','9'};
        StringBuffer output = new StringBuffer();
        for (int i=1; i<=str.length; i++) {
            output.append(str[i-1]);
            if (i%3==0 && i<str.length) {
                output.append(",");
            }
        }
        System.out.println(output);
    }

}

如果您想生成带格式的数字,请阅读此http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html

答案 1 :(得分:0)

输出:

Final String
123,456,789

这是代码


public class Comma {
    char[] str = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    char[] buf = new char[20];
    int size = str.length;
    int c = 1;

public void insert() { for (int i = 0; i < size; i++) { if (c % 4 == 0) { buf[c - 1] = ','; i--; } else { buf[c - 1] = str[i]; } c++; } System.out.println("Final String"); for (int i = 0; i < buf.length; i++) { System.out.print(buf[i]); } } public static void main(final String args[]) { Comma c = new Comma(); c.insert(); }
public void insert() { for (int i = 0; i < size; i++) { if (c % 4 == 0) { buf[c - 1] = ','; i--; } else { buf[c - 1] = str[i]; } c++; } System.out.println("Final String"); for (int i = 0; i < buf.length; i++) { System.out.print(buf[i]); } } public static void main(final String args[]) { Comma c = new Comma(); c.insert(); }

答案 2 :(得分:0)

        int j = 0;
        for(int i=0;i<size;i++)
        {
              c++;
              if(c%3==0)
              {
                buf[j] = ',';
                j++;
              }
              buf[j]=str[i];
              j++;
         }  

答案 3 :(得分:0)

请试试这个:

 int c=0;
  for(int i=0;i<size;i++)
  {
       if((i+1)%3==0)
       {
         buf[c] = ',';
         c++;
       }
     buf[c]=str[i];
    c++;
   }  

答案 4 :(得分:0)

我假设你想要从左边添加逗号:你想要“23,304”,而不是“233,04”。

List<Character> chars = new LinkedList<Character>(Arrays.asList('1', '2', '3', '4', '5', '6', '7', '8', '9'));
int positionForNewComma = chars.size() - 3;
while (positionForNewComma > 0) {
chars.add(positionForNewComma, ',');
    positionForNewComma -= 3;
}
System.out.println(chars);

如果您从右侧添加逗号,则改为:

List<Character> chars = new LinkedList<Character>(Arrays.asList('1', '2', '3', '4', '5', '6', '7', '8', '9'));
int positionForNewComma = 3;
while (chars.size() > positionForNewComma) {
    chars.add(positionForNewComma, ',');
    positionForNewComma += 3 + 1; // + 1 size the insertion moves the elements on the right
}
System.out.println(chars);

最好避免使用原始数组:[]。如果你确实使用了原始数组,那么看起来更像Java-ish来声明它们是“char []”而不是“char []”。