Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Programming 2. Task: Use nested for loops statements to draw empty boxes of

ID: 3667302 • Letter: J

Question

Java Programming

2. Task: Use nested for loops statements to draw empty boxes of any character (input from user). The boxes have the same number of rows and columns (input from the user; valid range: 5 to 21). Test for errors in input (including type)

SAMPLE OUTPUT:

Do you want to start(Y/N): y

How many chars/last row? n

Not an integer! Try again! How many chars/last row? fgfgfg

Not an integer! Try again! How many chars/last row? 7.6

Not an integer! Try again! How many chars/last row? 34

ERROR! Valid range 5 - 21. How many chars/last row? 7

What character? k

kkkkkkk

k     k

k     k

k     k

k     k

k     k

kkkkkkk

Do you want to continue(Y/N): y

How many chars/last row? y

Not an integer! Try again! How many chars/last row? 9

What character? #

#########

#       #

#       #

#       #

#       #

#       #

#       #

#       #

#########

Do you want to continue(Y/N): n

Explanation / Answer

import java.util.Scanner;
import java.io.*;

public class drawRectangle {

public static void main(String[] args){
  
   String c,pc,comc;
   int f=0, rows;
   Scanner input = new Scanner(System.in);
   System.out.print("Do you want to start(Y/N) :");
   c=(input.next());
   comc="Y";      
      
       while(c.compareTo(comc)==0)
       {

               System.out.print("How many chars/last row? :");
                  
               while (!input.hasNextInt())
               {      
                   input.next();
                   System.out.print("Not an integer! Try again! How many chars/last row?"); // Re-prompt
               }
               rows=input.nextInt();
              
               /* while (!(rows >= 5 && rows <= 21))
               {      
                   input.next();
                   System.out.print("Valid range 5 - 21. How many chars/last row"); // Re-prompt
               }*/

               while(rows >= 5 && rows <= 21)
               {
              
              

                  System.out.print("What Character? :");
                  pc=(input.next());
                  
                  for( int y = 1; y <= rows; y++ )
                   {
                           System.out.print(pc);
                           if( y == rows )
                           {   // Leave loop
                               break;
                           }
                   }
                   System.out.println();
                   for( int x = 3; x <= rows; x++ )
                   {                  
                           System.out.print(pc);
                           for( int z = 1; z <= rows/2; z++ )
                           {
                               System.out.print( " " );
                           }
                           System.out.println(pc);
                   }
                   for( int y = 1; y <= rows; y++ )
                   {
                           System.out.print(pc);
                           if( y == rows )
                           {
                               // Leave loop
                               break;
                           }
                   }
                   System.out.println();  
                   break;
               }
               System.out.print("Do you Continue (Y/N) :");
                c=(input.next());
  
              
       }
          
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote