473,461 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

println whitespace

mickey0
142 100+
Hello,
is there any chance to do it in a fashion way?
Expand|Select|Wrap|Line Numbers
  1. for (int i=5; i >= 0; i--) {
  2.   System.out.prinln( space*i + "Hello world!");
  3. }        
  4.  
I want get this:
Expand|Select|Wrap|Line Numbers
  1.      Hello world!
  2.    Hello world!
  3.   Hello world!
  4.  Hello world!
  5. Hello world!
  6.  
thanks
Aug 21 '10 #1
7 2997
TheServant
1,168 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. for (int i=5; i>=0; i--) {
  2.   for (int j=i; j>=1; j--) {
  3.     System.out.print(" ");
  4.   }
  5.   System.out.println( "Hello world!"); /* NOTICE you misspelled print */
  6. }
Aug 22 '10 #2
mickey0
142 100+
sorry, I was lloking for something general to do this and hopefully work around with the "printf"; in fact what about if I needed the opposite?
Expand|Select|Wrap|Line Numbers
  1. Hello!
  2.  Hello!
  3.   Hello!
  4.    Hello!
  5.  
Thanks
Aug 22 '10 #3
TheServant
1,168 Expert 1GB
To increase spaces:
Expand|Select|Wrap|Line Numbers
  1.     public static void example(Integer num) {
  2.         for (int i = num; i >= 0; i--) {
  3.             for (int j = i; j < num; j++) {
  4.                 System.out.print(" ");
  5.             }
  6.             System.out.println("Hello world!");
  7.         }
  8.     }
  9.  
  10.     public static void main(String[] args) {
  11.         example(5);
  12.     }
To reduce spaces:
Expand|Select|Wrap|Line Numbers
  1.     public static void example(Integer num) {
  2.         for (int i = num; i >= 0; i--) {
  3.             for (int j = i; j > 0; j--) {
  4.                 System.out.print(" ");
  5.             }
  6.             System.out.println("Hello world!");
  7.         }
  8.     }
  9.  
  10.     public static void main(String[] args) {
  11.         example(5);
  12.     }
Probably be able to make that a lot more efficient, but you get the idea.
Aug 22 '10 #4
chaarmann
785 Expert 512MB
A shorter way:
Expand|Select|Wrap|Line Numbers
  1. for (int i=5; i>=0; i--) { 
  2.   System.out.println("     ".substring(i) + "Hello world!"); /* NOTICE: 5 spaces */ 
  3. }
Aug 23 '10 #5
TheServant
1,168 Expert 1GB
That's a great idea chaarmann!
Aug 23 '10 #6
mickey0
142 100+
yes, but in that way I can't see I cood get the opposite:
Expand|Select|Wrap|Line Numbers
  1. Hello
  2.  Hello
  3.    Hello
  4.  
Aug 23 '10 #7
TheServant
1,168 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. for (int i=0; i<=5; i++) {
  2.   System.out.println("     ".substring(i) + "Hello world!");
  3. }
Aug 23 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: f | last post by:
I am writing a java code generation tool. I use xml and xslt. But I have some problem using xsl:for-each. here is my xml <?xml version = "1.0"?> <CLASS package_name=".test"...
2
by: Wolfgang Jeltsch | last post by:
Hello, it is often convenient to insert whitespace into an XML document in order to format it nicely. For example, take this snippet of a notional DocBook XML document: <para> This is a...
7
by: stephane | last post by:
Hi, I must implement the method printLn of the class Thoraire. And I've done this, can someone tell me if I am right ?
2
by: Carlitos | last post by:
Hi there, A class in Xerces J-API (Java) called TextImpl contains a property that returns whether the text is ignorable whitespace...
2
by: Carlitos | last post by:
Hi there, A class in Xerces J-API (Java) called TextImpl contains a property that returns whether the text is ignorable whitespace...
0
by: Shan Plourde | last post by:
Hi everyone, I have been using various regular expressions with the ASP.NET RegularExpressionValidator for quite some time. In general it works very well. One of the common regex's that I use...
3
by: David Pratt | last post by:
Hi. I am splitting a string on a non whitespace character. One or more whitespace characters can be returned as items in the list. I do not want the items in the list that are only whitespace (can...
56
by: infidel | last post by:
Where are they-who-hate-us-for-our-whitespace? Are "they" really that stupid/petty? Are "they" really out there at all? "They" almost sound like a mythical caste of tasteless heathens that "we"...
9
by: amattie | last post by:
Does anyone have any idea on how I can strip the extra whitespace in the XML that shows up when I receive a response from an ASP.NET 2.0 webservice? This has been discussed before, but no one has...
13
by: Chaim Krause | last post by:
I am unable to figure out why the first two statements work as I expect them to and the next two do not. Namely, the first two spit the sentence into its component words, while the latter two...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.