【问题标题】:Removing comma at the end of output java在输出 java 的末尾删除逗号
【发布时间】:2011-11-23 21:06:16
【问题描述】:

我正在尝试编写一个基本应用程序,该应用程序将获取用户想要输入的数字数量,然后向用户询问数字,将数字保存在字符串数组中,然后以特定格式打印出来。

这是我到目前为止唯一的问题是我需要删除输出末尾的最后一个逗号,我确定我做错了......感谢任何帮助。

import java.util.Scanner;
public class info {
    public void blue(){
        Scanner sc = new Scanner(System.in);
        Scanner dc = new Scanner(System.in);
        System.out.println("how many numbers do you have?");
        int a = dc.nextInt();
        System.out.println("enter your numbers");

        String index[]=new String [a];
        String i;

        for(int k = 0;k<index.length;k++){
            i = sc.nextLine();
            index[k]=i;
            }

            System.out.print("query (");

            for(int l = 0;l<index.length;l++){
            System.out.printf("'%s',",index[l]);
            }

            System.out.print(")");
    }
}

【问题讨论】:

标签: java arrays


【解决方案1】:

更清洁的方法可能是,

String comma="";

for(int l = 0; l<index.length; l++)
{
   System.out.printf("'%s''%s'", comma, index[l]);
   // Now define comma
   comma = ",";
}

【讨论】:

  • 我相信您对printf 的参数需要颠倒。现在,“a,b,c”将打印为“ab,c”
  • @DilumRanatunga 感谢您的关注。
  • 优秀的答案(因为它几乎和我的一样)。除非您需要通过循环第一次初始化逗号,即您应该以 String comma=""; 开头不仅仅是字符串逗号;
  • @Jay 做出了改变.. 抱歉我是 Java 新手.. 你应该考虑投票给答案 ;)
【解决方案2】:

更简单的是:

String conj="";
for(int l = 0;l<index.length;l++){
  System.out.print(conj);
  System.out.print(index[l]);
  conj=",";  
}  

你必须经常做这种事情,我经常写一个小“木匠”类:

public class Joiner
{
  private String curr;
  private String conj;
  private StringBuilder value;

  public Joiner(String prefix, String conj)
  {
    this.curr=prefix;
    this.conj=conj;
    value=new StringBuilder();
  }
  public Joiner(String conj)
  {
    this("", conj);
  }
  public Joiner append(String s)
  {
    value.append(curr).append(s);
    curr=conj;
    return this;
  }
  public String toString()
  {
    return value.toString();
  }
}

然后您可以使用以下代码构建连接字符串:

Joiner commalist=new Joiner(",");
for (String one : many)
{
  commalist.append(one);
}
System.out.println(commalist.toString()); // or whatever you want to do with it

或者喜欢建一个WHERE子句:

Joiner where=new Joiner(" where ", " and ");
if (foo!=null)
{
  where.append("foo="+foo);
}
if (bar!=null)
{
  where.append("bar="+bar);
}
String sql="select whatever from wherever"+where;

【讨论】:

    【解决方案3】:

    导入apache StringUtils,然后做

    StringUtils.join( index, "," );
    

    【讨论】:

      【解决方案4】:

      在 C# 领域有一段时间了,所以代码可能不准确,但这应该可以工作

      StringBuilder sb = new StringBuilder();
      for(int l = 0;l<index.length;l++){
        sb.Append(index[l] + ";");
      }
      sb.deleteCharAt(sb.Length - 1);
      System.out.print(sb.ToString());
      

      这将使您的代码不必一遍又一遍地写入输出,因此开销也更少。

      【讨论】:

      • ... 以重复字符串分配为代价。请改用 StringBuilder。
      【解决方案5】:
      }
      System.out.print("\b");//<---------
      System.out.print(")");
      

      为什么不用String缓冲区,最后输出呢?

      【讨论】:

        【解决方案6】:

        你可以使用String的substring方法来去掉最后的逗号。在循环中进行连接时,最好使用 StringBuilder.append。

        【讨论】:

        • 我认为您的意思是 StringBuilder 而不是 StringBuffer。我在他的代码中看不到任何暗示需要线程安全的开销。但是话虽如此,考虑到他的应用程序的规模,可能 StringBuffer 或 StringBuilder 的使用应该归类为不必要的优化,因为速率限制步骤不会是创建字符串对象,而是输入用户文本。
        • 已更正。我只是指出,与其调用多个 sys 输出,不如构造所需的字符串并立即输出。
        【解决方案7】:

        这是在自己的行上显示每个字符串,末尾有一个逗号。如果要将所有单词放在一行中,用逗号分隔,则替换

        for(int l = 0;l<index.length;l++){
            System.out.printf("'%s',",index[l]);
        }
        

        StringBuilder buf = new StringBuilder();
        
        for(int l = 0;l<index.length;l++){
           buf.append(index[l]);
        
           if (l != (index.length - 1)) {
               buf.append(",");
           }
        }
        
        System.out.println(buf.toString());
        

        【讨论】:

          【解决方案8】:

          这不需要if 检查数组中的每个元素:

          if(index.length > 0) {
              for(int l = 0;l<index.length-1;l++){
                  System.out.printf("'%s',",index[l]);
              }
              System.out.printf("'%s'",index[index.length-1]);
          }
          

          【讨论】:

            【解决方案9】:
             Arrays.toString(array).replaceAll("[\\[\\]]", "");
            

            【讨论】:

              【解决方案10】:
              for(int l = 0;l<index.length;l++){
                  if(l==index.length-1)
                      System.out.printf("'%s'",index[l]);
                  else
                      System.out.printf("'%s',",index[l]);
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2022-11-13
                • 1970-01-01
                • 1970-01-01
                • 2012-05-20
                • 2013-08-30
                • 1970-01-01
                相关资源
                最近更新 更多