I am trying to write a program where the user can enter a height and width and it creates a hollow box of asterisks. I can get the stars to print out but what is wrong with my code that it is putting them in weird lines. How do I get them to form into what I want them to?

import java.util.Scanner;

public class Stars
{
    public static void main(String args[])
    {
        // Create a Scanner so we can get input from the user.
        Scanner input = new Scanner(System.in);
        int starsinarow ; //number of stars up and down 
        int columns ; //how many across 
        int columncounter=0; //column counter 
        int rowCounter=0; // row counter
        int starsinarowCounter=0; 

        //welcome
        System.out.println("Welcome! This program creates a box made of stars! The rows and columns must be postive full numbers");
        System.out.println("Please enter the number of rows you want: ");
        starsinarow = input.nextInt ();
            if (starsinarow<0)
            {
                System.out.println("Please enter a vaild number.");
            }
            else
            {
                System.out.println("Please enter how many columns you want: ");
                columns = input.nextInt();

                //getting the top row
                while(columncounter<columns)
                {
                    System.out.print("* "); 
                    columncounter ++;
                }


                //printing middle stuff
                while (rowCounter<starsinarow-2)
                {
                    System.out.println("* ");
                    if(columns>2)
                    {
                        while (columncounter< columns);
                        {
                        System.out.print(" ");
                        columncounter++;
                        }
                    }
                    System.out.print("*");          
                    rowCounter ++; 
                }


                //getting the bottom row
                while(columncounter<columns)
                {
                    System.out.print("*");  
                    columncounter ++;
                }


            }       
    }
}

whats happeneing on the comman prompt:

C:\Users\jorda\Documents\CSCI201\Assignment3>java Stars
Welcome! This program creates a box made of stars! The rows and columns must be postive full numbers
Please enter the number of rows you want:
6
Please enter how many columns you want:
7
* * * * * * * *
**
**
**
*
C:\Users\jorda\Documents\CSCI201\Assignment3>java Stars
Welcome! This program creates a box made of stars! The rows and columns must be postive full numbers
Please enter the number of rows you want:
8
Please enter how many columns you want:
3
* * * *
**
**
**
**
**
*
C:\Users\jorda\Documents\CSCI201\Assignment

Edited 1 Year Ago by pritaeas: Moved to software development

Hmm, it's not ouputting the spaces on the middle rows.
Also, this:

//getting the bottom row
while(columncounter<columns)
{
    System.out.print("*");  
    columncounter ++;
}

Should be the same as this:
It's not outputting the bottom row properly because there's no space after each asterix.

//getting the top row
while(columncounter<columns)
{
    System.out.print("* "); 
    columncounter ++;
}

Could you try putting a tab instead of a space in the middle rows, just to see if that makes a difference?
ie System.out.print("\t")

Edited 1 Year Ago by humorousone

I updated the code:

import java.util.Scanner;

public class Stars
{
    public static void main(String args[])
    {
        // Create a Scanner so we can get input from the user.
        Scanner input = new Scanner(System.in);
        int starsinarow ; //number of stars up and down 
        int columns ; //how many across 
        int columncounter=0; //column counter 
        int rowCounter=0; // row counter
        int starsinarowCounter=0; 

        //welcome
        System.out.println("Welcome! This program creates a box made of stars! The rows and columns must be postive full numbers");
        System.out.println("Please enter the number of rows you want: ");
        starsinarow = input.nextInt ();
            if (starsinarow<0)
            {
                System.out.println("Please enter a vaild number.");
            }
            else
            {
                System.out.println("Please enter how many columns you want: ");
                columns = input.nextInt();

                //getting the top row
                while(columncounter<columns)
                {
                    System.out.print("* "); 
                    columncounter ++;
                }


                //printing middle stuff
                while (rowCounter<starsinarow-2)
                {
                    System.out.println("* ");
                    if(columns>2)
                    {
                        while (columncounter< columns);
                        {
                        System.out.print("\t");
                        columncounter++;
                        }
                    }
                    System.out.print("*");          
                    rowCounter ++; 
                }


                //getting the bottom row
                while(columncounter<columns)
                {
                    System.out.print("* "); 
                    columncounter ++;
                }


            }       
    }
}

but it still doesn't print a bottom line and only moved both asterisks over to the otherside:
columns:3
rows:4

* * * *
        **
        *

i updated the code again:

                //printing middle stuff
                System.out.println("* ");
                while (rowCounter<starsinarow-2)
                {
                    if(columns>2)
                    {
                        while (columncounter< columns);
                        {
                        System.out.print("\t");
                        columncounter++;
                        }
                    }
                    System.out.print("*");          
                    rowCounter ++; 
                }

and got this:

* * * *
        *       *

so i tried this:

                //getting the top row
                System.out.print("  ");
                while(columncounter<columns)
                {

                    System.out.print("* "); 
                    columncounter ++;
                }

and got

* * * *
*        *

You have 3 while loops using columncounter, and in each case you forgot to initialise/reinitialise columncounter before entering the loop(s).

This article has been dead for over six months. Start a new discussion instead.