Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes program to print square Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "program to print square" Watch "program to print square" New topic
Author

program to print square

karthik swamy
Ranch Hand

Joined: Mar 14, 2011
Posts: 39
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

Joined: Aug 15, 2010
Posts: 230
    
    2

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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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  


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: program to print square
 
Similar Threads
for loop
spiral traversal of mxn matrix
help on input character into screen
Loops??
JTextArea