• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays  RSS feed

 
Iliyan Iliev
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I'm new here and I want to apologize if there is something wrong with my post. So here is my code:
Can please someone tell me why, why I get this:
1
1
1
1
1
1
-1
-1
-1
-1
-1
-1
-1
1
1
1
1
1
1
1
-1
-1
-1
-1
-1
-1
-1
instead of this:
1 -1 1 -1 1 -1 1
1 -1 1 -1 1 -1 1
1 -1 1 -1 1 -1 1
1 -1 1 -1 1 -1 1
What am I doing wrong? Ofc, this is my homework as and I do not want to write it for me, just give me advices and explain me why my code works this way. I would be very grateful
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilyan,
Welcome to CodeRanch! The code tags got mangled so I fixed that for you.

You are really close. The main problem is that each number is being printed on its own line. println() prints and goes to the next line. If you change println() to print(), that will get rid of the new lines. You'll then have all the numbers printed out on one line. Which isn't what you want either, but a step in the right direction. Once you have that, see where you can add a System.out.println() statement to get the line breaks where you want them.
 
Iliyan Iliev
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ty for the reply, so I added this in 14 line System.out.println("\n") ; and my code worked! But what System.out.println("\n") does?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iliyan Iliev wrote:ty for the reply, so I added this in 14 line System.out.println("\n") ; and my code worked! But what System.out.println("\n") does?

It prints the "character" '\n', which is, in fact, a control character called a "newline"; which is a bit like the carriage-return on an old typewriter - it doesn't print anything, but it puts all characters that come after it on a new line.

It's probably also worth mentioning that
System.out.println()
will do the same thing, so the "\n" is redundant. In fact, you may be seeing TWO lines between everything (not sure, TBH).

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Despite what you find in the books, you should only write \n if you are told to produce an LF character. Use println instead.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!