Snarf
Snarf

Reputation: 2361

Removing comma at the end of output java

I'm trying code a basic application that will take the amount of numbers the user wants to input and then ask the user for the numbers, save the the numbers in a string array and then print them out in specific format.

this is what I have so far the only problem is I need to remove the last comma at the end of the output, I'm sure I'm going about this wrong... any assistance is appreciated.

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

Upvotes: 0

Views: 4285

Answers (10)

tryurbest
tryurbest

Reputation: 1429

Much cleaner approach could be,

String comma="";

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

Upvotes: 2

Jay
Jay

Reputation: 27464

Much simpler is:

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

You have to do this sort of thing so often, I routinely write a little "joiner" class:

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

Then you can build a concatenated string with code like:

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

Or like to build a WHERE clause:

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;

Upvotes: 1

digitaljoel
digitaljoel

Reputation: 26574

import apache StringUtils, then do

StringUtils.join( index, "," );

Upvotes: 1

DJ Quimby
DJ Quimby

Reputation: 3699

Been in the C# world for a while so the code may not be exact, but this should work

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

This will save your code from having to write to the output over and over, so there is less overhead as well.

Upvotes: 1

WIZARDELF
WIZARDELF

Reputation: 3885

}
System.out.print("\b");//<---------
System.out.print(")");

Why not use a String buffer and output it in the end?

Upvotes: 1

Narendra Yadala
Narendra Yadala

Reputation: 9664

You can use substring method of String to strip the comma in the end. It is also better to use StringBuilder.append when doing concatenation in a loop.

Upvotes: 1

NRitH
NRitH

Reputation: 13893

This is displaying each string on its own line, with a comma at the end. If you want all of the words on one line, separated by commas, then replace

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

with

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

Upvotes: 1

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

This requires no if check for each element in your array:

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

Upvotes: 2

dacwe
dacwe

Reputation: 43504

 Arrays.toString(array).replaceAll("[\\[\\]]", "");

Upvotes: 3

Danny
Danny

Reputation: 7518

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

Upvotes: 5

Related Questions