• Post Reply Bookmark Topic Watch Topic
  • New Topic

Controling output to console within a for loop  RSS feed

 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code is doing what i want it to do, but the output is not exactly right, i need the ones and tens to line up. Have tried different versions of the println method and also the printf method but no luck.
could someone point me in the right direction, or give a suggestion please.

current output
#1: 1 is odd
#2: 4 is even
#3: 9 is odd
#4: 16 is even
#5: 25 is odd
#6: 36 is even
#7: 49 is odd
#8: 64 is even
#9: 81 is odd




 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use printf()
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that worked perfectly, but what are the 2 and the 4 doing?
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
% [optional  number] d
The optional number specifies the minimum width in columns that should be used to display the given value. If the value that would normally be displayed is less than that width then the value will be padded on the left side with spaces until that width is arrived at. Note that if the value exceeds the width then the full value will be displayed, taking more than the specified amount of columns.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it, thanks for your insight.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use the ternary operator, you can even do that in a single line of code:
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:If you use the ternary operator, you can even do that in a single line of code:

i like this, it saves me from having to do an if else.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rick pine wrote:i like this, it saves me from having to do an if else.

Technically, the ternary operator *is* an if-then-else construct. You do save yourself a few lines of code because the expression is a more succinct form of conditional branch/return.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!