Avoid printing the last comma

前端 未结 13 521
无人及你
无人及你 2020-12-03 14:58

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

相关标签:
13条回答
  • 2020-12-03 15:26

    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);
          }
        }
      }
    }  
    
    0 讨论(0)
  • 2020-12-03 15:27

    Use StringBuilder for it. use substring based index.

    public class arraySort {
        public static void main(String[] args) {
            int a[] = { 2, 7, 5, 6, 9, 8, 4, 3, 1 };
            for (int i = 0; i < a.length; i++) {
                for (int j = i + 1; j < a.length; j++) {
                    if (a[i] < a[j]) {
                        int temp;
                        temp = a[i];
                        a[i] = a[j];
                        a[j] = temp;
                    }
                }
            }
    
            System.out.print("Descending order:{");
            StringBuilder br = new StringBuilder();
            for (int i = 0; i < a.length; i++) {
                br.append(a[i] + ",");
            }
            System.out.print( br.substring(0, br.length()-1));
            System.out.println("}");
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-03 15:28

    Clunky solution, but hey - it works.

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

    Alternatively, this works too.

    public static void atob(int a, int b){
     int count = 0;
      for(int i = a; i <= + b; i++) {
        if(count > 0){
          System.out.print(", ");
        }
           System.out.print(i);
          count++;
       }
    }
    
    0 讨论(0)
  • 2020-12-03 15:29

    Found another simple way using regex:

    String str = "abc, xyz, 123, ";
    str = str.replaceAll(", $", "");
    System.out.println(str);  // "abc, xyz, 123"
    
    0 讨论(0)
  • 2020-12-03 15:31

    You can simply use escape sequence for backspace(\b),outside the main loop,it will delete the last comma.

    public static void atob(int a,int b)
            {
                for(int i = a; i <= + b; i++) 
                {
                        System.out.print(i + ",");
                }
                System.out.print("\b");
            }
    
    0 讨论(0)
  • 2020-12-03 15:33

    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)

    0 讨论(0)
提交回复
热议问题