This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Printing a cross evenly spaced. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing a cross evenly spaced." Watch "Printing a cross evenly spaced." New topic
Author

Printing a cross evenly spaced.

Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

The homework assignment says:

24. Write a program that inputs N, where 3  <= N <= 25, and outputs a cross. The following figure shows the output when N is 5.


I found this thread that seems to be trying to do the same that I am, but I'm running into the problem where the middle/center character of the cross is off a little bit. I don't think it matters when I turn the assignment in, but it was bugging me and I wanted to know if there were ways to fix it. I've looked into formatting a little bit with Dates and DecimalFormat, but I'm not sure how I would implement it here. Any tips would be appreciated. Also as a side note I think I picked up on what marc weber was hinting at, in the thread, with the second half of the "if statement", but I'm wondering did I get it right?

Here's my code:


Output:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37900
    
  22
Welcome to the Ranch

I am not convinced that your name for your class is the best name ever seen in history. I also don’t like the endless loop and break.
I think you are going to have to go back to the pencil and paper stage, and work out exactly how you get from number of rows to whether you print X or not. I would suggest you start by printingandThen you have to work out how to get the two together. I think you are not far off, actually. Also, try spaces instead of tabs.
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

Your logic seems to be good. Replace the "\t" with some another character and have a look.


Do not wait to strike till the iron is hot; but make it hot by striking....
Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

Hey thanks for the replies! I agree the class name isn't the best for real world use, but it was an easy way for me to figure out what it was for. That and we haven't gone over proper class names yet as our teachers focusing on just teaching us to get our code to run.

Replacing the "\t" with a " " seems to have worked as I now get this:


I still get an issue when I increase the number of spaces though.
2 spaces:

3 spaces:


It seems to grow the more spaces I add. I think the reason why the spacing of the middle X is off is that it prints one character for "X", but it's printing 2+ characters when I increase the spaces larger than 1. This causes it to be offset by a little bit and more so the more spaces I add. I guess the way to fix it would be to format, but it could get a little tricky trying to maintain the cross shape as well. I'll give it a try and post what I figure out here. Thanks for the help so far.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37900
    
  22
Try "X " instead of "X".
Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

Adding spaces after the X helps. I now get:


I think the reason why I was wanting to format the output was so that it could change dynamically, but now that I think about it I don't need to worry about it because the user will never change the spaces only the amount of lines with X's. Thanks for all the help though!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37900
    
  22
Well done
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing a cross evenly spaced.
 
Similar Threads
print invalid selction not working
Need advice on my first program
Multiplication Table
2d Array problem gasprices
do {} while statement illegal start of type