• Post Reply Bookmark Topic Watch Topic
  • New Topic

Formatting tables  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the last statement in this code System.out.println(); change the format of the table. Before implemented the numbers are in one horizontal line. After adding this statement the table develop rows.




 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean you get it all in one row before you insert the System.out.println(); call? Well, that call does this, so that should explain your problem.

You didn't have the code tags in the right place. I corrected that and removed some of the unnecessary blank lines. Put the [/code] after the code block.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Well, that call does this, so that should explain your problem.

@Chago: it also explains that it does not use '\n', which is generally bad practise, as it will not always show a line break on Windows systems.

For your line 12, for example, you're much better off using:
System.out.printf("%n-----------------------------------%n");
or:
System.out.println();
System.out.println("-----------------------------------");
than:
System.out.println("\n-----------------------------------");

HIH

Winston
 
Chago Bowers
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank both of you for your response. So in summary, the System.out.println(); command will take any previous System.out.print commands that are in parenthesis and print them on their own line? I just want to make sure I'm understanding how and what it is doing to the previous commands
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
println() merely prints a newline. That means any output that follows it will go on the next line (relative to everything that came before it).
 
Chago Bowers
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no output that followed the command. It is the last command in the code, but when implemented changed the format of the output of the preceding code. When removed from the code the output format for the code is just a straight line. See below:

Without System.println (); at the end of the code
Multiplication Table
1 2 3 4 5 6 7 8 9
-----------------------------------------
1 | 1 2 3 4 5 6 7 8 92 | 2 4 6 8 10 12 14 16 183 | 3 6 9 12 15 18 21 24 274 | 4 8 12 16 20 24 28 32 365 | 5 10 15 20 25 30 35 40 456 | 6 12 18 24 30 36 42 48 547 | 7 14 21 28 35 42 49 56 638 | 8 16 24 32 40 48 56 64 729 | 9 18 27 36 45 54 63 72 81

With System.println (); at the end of the code

Multiplication Table
1 2 3 4 5 6 7 8 9
-----------------------------------------
1 | 1 2 3 4 5 6 7 8 9
2 | 2 4 6 8 10 12 14 16 18
3 | 3 6 9 12 15 18 21 24 27
4 | 4 8 12 16 20 24 28 32 36
5 | 5 10 15 20 25 30 35 40 45
6 | 6 12 18 24 30 36 42 48 54
7 | 7 14 21 28 35 42 49 56 63
8 | 8 16 24 32 40 48 56 64 72
9 | 9 18 27 36 45 54 63 72 81
 
Chago Bowers
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to put the "out" in the previous post. I mean System.out.println ();
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it changes the output in that it inserts a newline - thus forcing all following output on the next line. "Following" could be whatever gets printed in the next iteration of the loop.

If no "println" is ever issued, then all output goes onto the same line.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chago Bowers wrote:There is no output that followed the command...

Perhaps the easiest way to think of it is this:
System.out.println() == System.out.print("\n")

Except that it doesn't necessarily print out '\n'; it prints out whatever 'newline' is applicable for the system it's running on (which on Windows is actually two characters: "\r\n").

and:
System.out.println("whatver");
is equivalent to:
System.out.print("whatever");
System.out.println();


HIH

Winston
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!