CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Join Date
    Jul 2007
    Posts
    273

    print white space

    Hello,
    is there any chance to do it in a fashion way?

    Code:
    		for (int i=5; i >= 0; i--) {
    			System.out.prinln( space*i + "Hello world!");
    		}
    I want get this:
    Code:
         Hello world!
       Hello world!
      Hello world!
     Hello world!
    Hello world!
    thanks

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: print white space

    A techique to generate a String of blanks:
    Have a String of enough blanks/spaces for the largest request
    Use the substing method with an expression based on i to extract as many spaces as are needed for this usage.
    Norm

  3. #3
    Join Date
    Jul 2007
    Posts
    273

    Re: print white space

    Quote Originally Posted by Norm View Post
    A techique to generate a String of blanks:
    Have a String of enough blanks/spaces for the largest request
    Use the substing method with an expression based on i to extract as many spaces as are needed for this usage.
    I don't understand what you mean; only thing I got is use StringBuilder s = new StringBuilder(); and then do s.setLength(s.length()-1); for each iteration.
    but I need somethin smater and at low level

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: print white space

    Something like this:
    Code:
    "                                 ".substring(0, i);
    Norm

  5. #5
    Join Date
    Jul 2007
    Posts
    273

    Re: print white space

    OK, isn't there a way to don't use substring method ? I mean to use just the 'i' ?

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: print white space

    i is an int. What you want is a String of blanks. I don't know how to convert an int to a String of blanks.

    Perhaps you are thinking of the printf() method. It has a way of formatting data that could include leading blanks.
    Norm

  7. #7
    Join Date
    Jul 2007
    Posts
    273

    Re: print white space

    Quote Originally Posted by Norm View Post
    i is an int. What you want is a String of blanks. I don't know how to convert an int to a String of blanks.

    Perhaps you are thinking of the printf() method. It has a way of formatting data that could include leading blanks.
    yes, I can change it, i've already seen it but don't know how formatting it...
    EDIT: can Regex even help? Don't know how BTW
    Last edited by mickey0; August 21st, 2010 at 04:32 PM.

  8. #8
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: print white space

    regex is for pattern matching. I don't know if you can generate strings based on the value of an int using it.

    Write your own method to return the number of spaces you want:
    String spaces(int numberOfSpaces) { ...
    Norm

  9. #9
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: print white space

    The String.format method can do padding:
    Code:
    int length = 20;  // 20 spaces
    String foo = String.format("Start of padding:'%1$"+length+"s' end of padding", "");
    System.out.println(foo);
    To me programming is more than an important practical art. It is also a gigantic undertaking in the foundations of knowledge...
    Grace Hopper
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  10. #10
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: print white space

    Another option if you don't mind using a third-party library:

    org.apache.commons.lang.StringUtils.repeat

    You can get the library here:
    http://commons.apache.org/

  11. #11
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: print white space

    Yet another option is this code I posted several years ago.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  12. #12
    Join Date
    Aug 2010
    Posts
    3

    Thumbs up Re: print white space

    Here is the code snippet....

    =============================
    for(int i=5;i>=0;i--){
    String blankString = new String();

    for(int temp = i;temp>0;temp--){
    blankString = blankString+" ";
    }
    System.out.println(blankString+"hello world");
    }
    }
    =============================

  13. #13
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: print white space

    for(int temp = i;temp>0;temp--){
    blankString = blankString+" ";

    Waste of CPU time.
    Norm

  14. #14
    Join Date
    Jul 2007
    Posts
    273

    Re: print white space

    OK thanks but I need something different:
    Code:
    String createWhiteSpace(int num) {
       String spaces = .........................
       return spaces;
    }
    
    //main
    //let args from command line to be '10'
    System.out.println("|" + creatingWhiteSpace(args[0]) + "|" );
    Is there a compact way to create a white space string in a shot inside "createWhiteSpace(int num)" ? I thight to use stringFormat even I don't know how; other ideas? (compact, please)
    thanks

  15. #15
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: print white space

    Why compact? What is the requirement for compact?

    args from command line to be "10" <<<<- note args is a String array, not an int
    Last edited by Norm; August 29th, 2010 at 04:56 PM.
    Norm

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured