Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Printing a cross evenly spaced.

Alex Petsche
Ranch Hand
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
Posts: 50768
83
• 1
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
Posts: 198
• 1
Your logic seems to be good. Replace the "\t" with some another character and have a look.

Alex Petsche
Ranch Hand
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
Posts: 50768
83
• 1
Try "X " instead of "X".

Alex Petsche
Ranch Hand
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
Posts: 50768
83
Well done