Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By kjkrum

Thread: Deleting spaces in java code

  1. #1
    danielki is offline Member
    Join Date
    Feb 2014
    Posts
    16
    Rep Power
    0

    Default Deleting spaces in java code

    Hello,
    my method below works fine to print a matrix but when it prints every row,
    it is printing extra 4 white spaces which is not required. How can I delete those
    extra spaces at the end? when I use System.out.print((matrix[i][j] + " ").replaceAll("^\\s+", ""));
    it trims every thing including the spaces I needed for my matrix. So where should I put replaceAll("^\\s+", "") ?
    Thanks!

    Java Code:
        private static void printMatrix(int[][] matrix) {  
                System.out.println();  
                System.out.println("Matrix:");  
                    for (int i = 0; i < matrix.length; i++) {  
                    for (int j = 0; j < matrix[i].length; j++) {  
                        System.out.print(matrix[i][j] + "    ");  
                          
                    }  
                    
                    System.out.println();  
                       
                }  
                
            }

  2. #2
    Cyrus is offline Member
    Join Date
    Mar 2014
    Posts
    18
    Rep Power
    0

    Default Re: Deleting spaces in java code

    Perhaps

    Java Code:
    System.out.print(matrix[i][j]);
    if (j < matrix[i].length - 1) System.out.println("    ");

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Deleting spaces in java code

    There are lots of ways to approach this. I usually output the first element outside the loop, then iterate from element 1:

    Java Code:
    int[] someArray;
    // ...
    System.out.print(someArray[0]);
    for(int i = 1; i < someArray.length; ++i) {
       System.out.print(", ");
       System.out.print(someArray[i]);
    }
    I can't really justify that... just a habit.
    danielki likes this.
    Get in the habit of using standard Java naming conventions!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,727
    Blog Entries
    7
    Rep Power
    21

    Default Re: Deleting spaces in java code

    Quote Originally Posted by kjkrum View Post
    There are lots of ways to approach this. I usually output the first element outside the loop, then iterate from element 1:

    Java Code:
    int[] someArray;
    // ...
    System.out.print(someArray[0]);
    for(int i = 1; i < someArray.length; ++i) {
       System.out.print(", ");
       System.out.print(someArray[i]);
    }
    I can't really justify that... just a habit.
    I normally avoide that test in the loop body like this:

    Java Code:
    int[] someArray;
    String cdr= "";
    for (int i= 0 i < someArray.length; i++) {
       System.out.print(cdr); cdr= ", ";
       System.out.print(someArray[i]);
    }
    I can't justify it either; my fingers type it without me doing any thinking ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. how do i count spaces in java?
    By MrPosotive in forum New To Java
    Replies: 13
    Last Post: 11-30-2012, 05:07 AM
  2. how do i count spaces in java?
    By MrPosotive in forum Advanced Java
    Replies: 0
    Last Post: 11-29-2012, 05:51 AM
  3. deleting spaces !!
    By young developer in forum New To Java
    Replies: 7
    Last Post: 03-30-2011, 04:11 PM
  4. Replies: 9
    Last Post: 03-06-2011, 04:00 AM
  5. Replies: 2
    Last Post: 02-06-2011, 05:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •