This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

program to print square

 
karthik swamy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone,
please tell me how to print square..
here is my code.




so please tell me where i am wrong.

Thanks
Karthik
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either change "5 " to "5" or " " to " ".
i.e. print either 2 (or 1 ) character(s) for both cases.

By the way, you are printing "5" for a 6x6 square instead of 5x5 ?
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The formatting lets Raymond down a bit, but he's right - when you print "5 " you print two characters, but when you print " " you only print one. As he suggests, either always print two ("5 " or "  "), or always print one ("5" or " ").

Raymond, in case you were wondering how I got the double space to work: by using the HTML escape code  
 
Campbell Ritchie
Sheriff
Pie
Posts: 50240
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, for reasons given here, avoid \n unless you specifically require the LF (\u000a) character. Don't write System.out.print("\n"); Write System.out.println(); You can use the %n tag instead.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic