Hello,
is there any chance to do it in a fashion way?
I want get this:Code:for (int i=5; i >= 0; i--) {
System.out.prinln( space*i + "Hello world!");
}
thanksCode:Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Printable View
Hello,
is there any chance to do it in a fashion way?
I want get this:Code:for (int i=5; i >= 0; i--) {
System.out.prinln( space*i + "Hello world!");
}
thanksCode:Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
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.
Something like this:
Code:" ".substring(0, i);
OK, isn't there a way to don't use substring method ? I mean to use just the 'i' ?
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.
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) { ...
The String.format method can do padding:To me programming is more than an important practical art. It is also a gigantic undertaking in the foundations of knowledge...Code:int length = 20; // 20 spaces
String foo = String.format("Start of padding:'%1$"+length+"s' end of padding", "");
System.out.println(foo);
Grace Hopper
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/
Yet another option is this code I posted several years ago.
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");
}
}
=============================
for(int temp = i;temp>0;temp--){
blankString = blankString+" ";
Waste of CPU time.
OK thanks but I need something different:
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)Code:String createWhiteSpace(int num) {
String spaces = .........................
return spaces;
}
//main
//let args from command line to be '10'
System.out.println("|" + creatingWhiteSpace(args[0]) + "|" );
thanks
Why compact? What is the requirement for compact?
args from command line to be "10" <<<<- note args is a String array, not an int
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.
A loop is not a HIT!
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:
For that you need to create the space method:Code:for (int i = 5; i >= 0; i--) {
System.out.println(space(i) + "Hello World!");
}
Code:String space(int i) {
return i == 0 ? "" : String.format("%" + i + "s", "");
}
Fine, nothing shorter than String.Format?
Is it possible to do this avoiding the exception while 'i' is zero?
I mean to change the StringFormat to avoid exception and not the for.Code:for (int i=5; i >=0; i--) {
System.out.println("Hello " + String.format("%" + i + "s", "") );
}
thanks
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.Quote:
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", "") );
}
BTW In the code above your spaces are after the "Hello" and not before it as you originally requested.
Add a conditional test for the case of i == 0: (condition) ? "" : format...
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.
try a simpler soultion:
Code:string.Empty.PadLeft(i, ' ');
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.Quote:
try a simpler soultion:
Yes you are right
must have been one of those days for me that I logged on here sleepy.
Don't worry, we all have days like that.