TextRectangles.java
1    /* 
2     * Program to draw rectangles of stars in the standard output stream. 
3     * The dimensions of the rectangle are read from the standard input stream. 
4     */
5    
6    package npw;
7    import java.util.Scanner;
8    
9    public class TextRectangles {
10       public static void main(String[] args) {
11           Scanner sc = new Scanner(System.in); //make a scanner to read input
12           System.out.print("Number of rows? ");
13           int nrOfRows = sc.nextInt(); //get rows from user
14           System.out.print("Number of columns? ");
15           int nrOfColumns = sc.nextInt(); //get columns from user
16           drawRectangle(nrOfRows, nrOfColumns); //draw the stars
17       }
18   
19       private static void drawRectangle(int nrOfRows, int nrOfColumns) {
20           int i = 1;
21           while(i <= nrOfRows) {
22               drawOneRow(nrOfColumns);
23               i++;
24           }
25       }
26   
27       private static void drawOneRow(int nrOfColumns) {
28           int i = 1;
29           String rowString = ""; //make an empty string to store stars for row
30           while(i <= nrOfColumns) {
31               rowString = rowString + "*"; //add a star for each column
32               i++;
33           }
34           System.out.println(rowString); //print the row to the console on an individual line
35       }
36   }
37