Fixing Print Square Error in Box Program

  • Thread starter Thread starter courtrigrad
  • Start date Start date
  • Tags Tags
    Change
Click For Summary
SUMMARY

The forum discussion addresses a common issue in Java programming related to printing a square using the Box class. The provided code successfully generates a square using 'x' characters for the borders and spaces for the interior. The problem arises when the output is viewed in a non-fixed-width font, which distorts the appearance of the square. The solution emphasizes the importance of using a fixed-width font for accurate visual representation.

PREREQUISITES
  • Basic understanding of Java programming
  • Familiarity with nested loops in Java
  • Knowledge of console output formatting
  • Experience with font types and their impact on output display
NEXT STEPS
  • Explore Java console output formatting techniques
  • Learn about fixed-width fonts and their applications in programming
  • Investigate debugging techniques for visual output issues in Java
  • Review best practices for structuring console applications in Java
USEFUL FOR

Java developers, programming students, and anyone troubleshooting console output formatting issues in their applications.

courtrigrad
Messages
1,236
Reaction score
2
Hello all

My program is trying to print out a square but it is not working. I figure something is wrong with the spacing.

Code:
 public class Box
{
  public void getBox( int a )
  {
      for( int i = 0; i < a; i++)
      {
          for( int j = 0; j < a; j++)
          {
             if( i == 0 || i == a-1)
              System.out.print("x");
             else
             if( j == 0 || j == a-1)
              System.out.print("x");
             else
              System.out.print( " " );
            }
              System.out.println( " " );
            
        }
    }
}

Any help is greatly appreciated

Thanks!
 
Computer science news on Phys.org
It should work. (In fact, it DOES work.) How are you invoking it?
 
How are you viewing the results? If the output is displayed in a non fixed width font, the spacing will appear wrong, even when correct.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
2
Views
3K
Replies
4
Views
3K