|
-
August 21st, 2010, 10:19 AM
#1
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
-
August 21st, 2010, 11:57 AM
#2
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
-
August 21st, 2010, 12:04 PM
#3
Re: print white space
 Originally Posted by Norm
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
-
August 21st, 2010, 12:30 PM
#4
Re: print white space
Something like this:
Code:
" ".substring(0, i);
Norm
-
August 21st, 2010, 02:42 PM
#5
Re: print white space
OK, isn't there a way to don't use substring method ? I mean to use just the 'i' ?
-
August 21st, 2010, 02:45 PM
#6
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
-
August 21st, 2010, 04:29 PM
#7
Re: print white space
 Originally Posted by Norm
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.
-
August 21st, 2010, 04:55 PM
#8
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
-
August 24th, 2010, 11:01 AM
#9
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.
-
August 24th, 2010, 11:48 AM
#10
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/
-
August 25th, 2010, 05:44 AM
#11
Re: print white space
Yet another option is this code I posted several years ago.
-
August 27th, 2010, 03:53 AM
#12
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");
}
}
=============================
-
August 27th, 2010, 06:52 AM
#13
Re: print white space
for(int temp = i;temp>0;temp--){
blankString = blankString+" ";
Waste of CPU time.
Norm
-
August 29th, 2010, 04:19 PM
#14
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
-
August 29th, 2010, 04:54 PM
#15
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
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
|