|
-
August 29th, 2010, 05:38 PM
#16
Re: print white space
 Originally Posted by Norm
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.
-
August 29th, 2010, 05:57 PM
#17
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
-
August 29th, 2010, 06:56 PM
#18
-
August 29th, 2010, 06:57 PM
#19
Re: print white space
 Originally Posted by mickey0
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", "");
}
-
August 30th, 2010, 05:03 AM
#20
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
-
August 30th, 2010, 06:29 AM
#21
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.
-
August 30th, 2010, 07:45 AM
#22
Re: print white space
Add a conditional test for the case of i == 0: (condition) ? "" : format...
Norm
-
September 1st, 2010, 10:52 AM
#23
Re: print white space
 Originally Posted by mickey0
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 [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.
-
August 30th, 2011, 08:12 PM
#24
Re: print white space
try a simpler soultion:
Code:
string.Empty.PadLeft(i, ' ');
-
August 31st, 2011, 10:52 AM
#25
Re: print white space
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.
-
August 31st, 2011, 06:11 PM
#26
Re: print white space
Yes you are right
must have been one of those days for me that I logged on here sleepy.
-
September 1st, 2011, 07:12 AM
#27
Re: print white space
Don't worry, we all have days like that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|