Is there an easy way to eliminate the final comma in my output? Number Seperator

Viewed 104

For another assignment i needed to program a "number seperator", that splits any given int value into all of its digits and returns it to the main class as a String.

I have the program up and running but there's a small problem with my output.

public class NumberSeperator {

   static String splitNumber(int zahl)  {
       String s = Integer.toString(zahl);
       return s;
   }

   public static void main(String args[]) {
       System.out.println("Input a Number: ");
       int zahl = readInt();

       String ziffern = splitNumber(zahl);
       for (int i = 0; i < ziffern.length(); i++) {
           System.out.print(ziffern.charAt(i) + ",");
       }
   }
}

The output of 1234 should be: 1,2,3,4 and the actual output is: 1,2,3,4,

At the risk of sounding extremely stupid, is there an easy fix to this?

6 Answers

How about printing first element without comma and rest in form ,nextElement like

one, two, three
^^^---------------- - before loop
   ^^^^^----------- - loop iteration
        ^^^^^^^---- - loop iteration

It can be achieved like:

if(ziffern.length()>0){
    System.out.print(ziffern.charAt(0));
}
for(int i=1; i<ziffern.length(); i++){
    System.out.print(", "+ziffern.charAt(i));
} 

OR you can convert ziffern to String[] array first and use built-in solution which is: String.join(delimiter, data)

System.our.print(String.join(",", ziffern.split("")));

When it's the last iteration, just don't add it.

In the last iteration, it will make the comma empty so that you won't see it after the last value.

String comma=",";
for (int i = 0; i < ziffern.length(); i++) {
      if (i == ziffern.length()-1) {
         comma="";
       }
       System.out.print(ziffern.charAt(i) + comma);
 }

with Java 8 and streams you can do it in a single command:

String join = Arrays.asList(ziffern.split(""))
                    .stream()
                    .collect(Collectors.joining(","));

System.out.println(join);

or with just plain java 8:

String join = String.join(",", ziffern.split(""));

System.out.println(join);

You can just output the string without the last character. Your modified code should be:

public class NumberSeperator {

   static String splitNumber(int zahl)  {
   String s = Integer.toString(zahl);
   return s;
   }

public static void main(String args[]) {

           int zahl = 1234;
           String s="";
           String ziffern = splitNumber(zahl);
           for (int i = 0; i < ziffern.length(); i++) {
               s+=ziffern.charAt(i) + ",";
           }
           System.out.println(s.substring(0,s.length()-1));
       }

Quite often this occurs when you know you have at least two items to print. So here is how you could do it then.

       String ziffern = splitNumber(zahl);
       String output = ziffern[0]; 
       for (int i = 1; i < ziffern.length(); i++) {
           output = "," + ziffern[i];
       }
       System.out.println(output);

A simple one liner will do your job:

static String splitNumber(int zahl)  {
    return String.join(",", String.valueOf(zahl).split(""));
}
Related