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

    Re: print white space

    Quote Originally Posted by Norm View Post
    Why compact? What is the requirement for compact?

    args from command line to be "10" <<<<- note args is a String array, not an int
    OK no problem; suppose a conversion after get it from command line; I just wanted put emphasis on the fact that I need a general way to do it (no assumptions on the size of the string)
    Requirements? I need something of a line to create it, just an instruction.

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

    Re: print white space

    To create a String of blanks of length n, concatenate some blank strings together until you have the length == n. Many ways to do this. Most involve a loop somewhere, either in your code or in the method that is called.
    Norm

  3. #18
    Join Date
    Jul 2007
    Posts
    273

    Re: print white space

    A loop is not a HIT!

  4. #19
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: print white space

    Quote Originally Posted by mickey0 View Post
    Requirements? I need something of a line to create it, just an instruction.
    Have you seen dlorde's post #9? His suggestion is a good solution. It's probably not as short as you want, but to get what (I think) you want you have to create a method, in which case I don't understand why so much insistence in it being "compact".

    To summarise, you want something "compact" you can use to insert a certain (unbounded) number of spaces in a String, for example:
    Code:
    for (int i = 5; i >= 0; i--) {
    	System.out.println(space(i) + "Hello World!");
    }
    For that you need to create the space method:
    Code:
    String space(int i) {
    	return i == 0 ? "" : String.format("%" + i + "s", "");
    }

  5. #20
    Join Date
    Jul 2007
    Posts
    273

    Re: print white space

    Fine, nothing shorter than String.Format?

    Is it possible to do this avoiding the exception while 'i' is zero?
    Code:
    for (int i=5; i >=0; i--) {
       System.out.println("Hello " + String.format("%" + i + "s", "")     );
    }
    I mean to change the StringFormat to avoid exception and not the for.

    thanks

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

    Re: print white space

    Is it possible to do this avoiding the exception while 'i' is zero?
    Code:
    for (int i=5; i >=0; i--) {
    System.out.println("Hello " + String.format("%" + i + "s", "") );
    }
    Good grief, why are you making something so simple into a nightmare. You have been told several times to create a method to return the spaces and yet you are still trying to put it into one line. If you use the method jcaccia gave you it handles a zero space.

    BTW In the code above your spaces are after the "Hello" and not before it as you originally requested.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

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

    Re: print white space

    Add a conditional test for the case of i == 0: (condition) ? "" : format...
    Norm

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

    Re: print white space

    Quote Originally Posted by mickey0 View Post
    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)
    I already showed you how to do it with String.format, and yes, it is 'compact' - though why that matters, I don't know.

    If you're going to ask for help, it makes sense and it's only polite to read and try to understand the replies - if you don't understand them, just ask.

    Why do we never have time to do it right, but always have time to do it over?
    Anon.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  9. #24
    Join Date
    May 1999
    Location
    G day Mayt Land
    Posts
    971

    Re: print white space

    try a simpler soultion:
    Code:
    string.Empty.PadLeft(i, ' ');

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

    Re: print white space

    try a simpler soultion:
    It would be simpler if it was Java code, unfortunately it's isn't any part of the JDK I know of. It does look suspiciously like .NET code though.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  11. #26
    Join Date
    May 1999
    Location
    G day Mayt Land
    Posts
    971

    Re: print white space

    Yes you are right
    must have been one of those days for me that I logged on here sleepy.

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

    Re: print white space

    Don't worry, we all have days like that.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Page 2 of 2 FirstFirst 12

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