Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, July 29, 2016

Beginner Java programmer: avoid printing the last comma

Beginner Java programmer: avoid printing the last comma


I'm trying to print this loop without the last comma. I've been Googling about this and from what i've seen everything seems overcomplex for such a small problem. Surely there is an easy solution to avoid printing the last comma. Much appreciated if somebody could help me out, this is driving me insane!!!

For example it loops from 1-10 // 1,2,3,4,5,6,7,8,9,10, < do not want this last comma

  public static void atob(int a,int b)              {                  for(int i = a; i <= + b; i++)                   {                          System.out.print(i + ",");                  }              }  

Answer by Farhan Ahmed for Beginner Java programmer: avoid printing the last comma


Try this

 public static void atob(int a,int b)              {                  for(int i = a; i < b; i++)                   {                          System.out.print(i + ",");                  }                          System.out.print(b);               }  

Answer by Aadi Droid for Beginner Java programmer: avoid printing the last comma


I might get stoned to death for this answer

public static void atob(int a,int b) {         if(a<=b) {             System.out.println(a);             for(int i = a+1;i<=b;i++) {                 System.out.println(","+i);             }         }  }    

Answer by Reg for Beginner Java programmer: avoid printing the last comma


public static void atob(int a, int b) {      if (a < b) {          System.out.print(a);          while (a < b) {              a++;              System.out.print("," + a);          }      }  }  

When called with

atob(0,10);  

Will give the output

0,1,2,3,4,5,6,7,8,9,10

Answer by Anish Dasappan for Beginner Java programmer: avoid printing the last comma


Try:

public static void atob(int a, int b) {      if (b < a) {          final int temp = a;          a = b;          b = temp;      }       System.out.print(a++);        for (int i = a; i < b ; i ++ ) {          System.out.print("," + i);      }  }  

Answer by Jochem for Beginner Java programmer: avoid printing the last comma


A general approach could be to make a distinction between the first item and all the others. The first run, no comma is printed BEFORE i. After that, a comma is printed before i, every time.

public static void atob(int a,int b) {      boolean first = true;      for(int i = a; i <= + b; i++) {          if ( first == false ) System.out.print(",");           System.out.print(i);          first = false;      }  }  

In this specific case, using the ternary operator (http://en.wikipedia.org/wiki/Ternary_operation), you could write it as compact as:

public static void atob(int a,int b) {      for(int i = a; i <= + b; i++) {          System.out.print( i + ( i < b ? "," : "" ) );       }  }  

Without ternary operation, this would look like this (and is more readable, which is often a good thing):

public static void atob(int a,int b) {      for(int i = a; i <= + b; i++) {          System.out.print( i );           if ( i < b ) System.out.print( "," );      }  }  

(note that + b is the same as b, so you could replace that, as others have done in their answers)

Answer by Amos M. Carpenter for Beginner Java programmer: avoid printing the last comma


With slight adjustments (method name, variables, and space after comma):

public static void printSequence(int start, int end) {    if (end < start) {      return; // or however you want to handle this case    }    if (end == start) {      System.out.print(start);      return;    }    StringBuilder sequence = new StringBuilder();    for (int i = start; i <= end; i++) {      sequence.append(i).append(", ");    }    // now simply print the sequence without the last ", "    System.out.print(sequence.substring(0, sequence.length() - 2));  }  

Answer by Peter Lawrey for Beginner Java programmer: avoid printing the last comma


Yet another way to do this.

String sep = "";  for(int i = a; i <= b; i++) {      System.out.print(sep + i);      sep = ",";  }  

if you are using a StringBuilder

StringBuilder sb = new StringBuilder();  for(int i = a; i <= b; i++)      sb.append(i).append(',');  System.out.println(sb.subString(0, sb.length()-1));  

Answer by charlie for Beginner Java programmer: avoid printing the last comma


Java 8:

IntStream.range(a, b + 1) // [a:b]           .collect(Collectors.joining(","));  

If you want to perform interleaving actions:

Java 8:

IntStream.range(a, b + 1) // [a:b]           .peek(System.out::print)           .limit(b - a) // [a:(b-1)]           .forEach(i -> System.out.print(","));  

Java 9:

IntStream.range(a, b + 1) // [a:b]           .peek(System.out::print)           .takeWhile(i -> i < b) // [a:(b-1)]           .forEach(i -> System.out.print(","));  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.