Java Programming 2. Task: Use nested for loops statements to draw empty boxes of
ID: 3667399 • 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
Given are the complete code along with comments:
import java.util.Scanner;
public class HelloWorld
{
public static void main(String[] args) {
Scanner input = new Scanner(System. in );
//VARIABLE DECLARATION//
char ans = 'n';
int row = 0;
char value = 'k';
do { // LOOP BEGIN//
System.out.println("DO YOU WANT TO START put Y OR N?");
ans = input.next().charAt(0);
if(ans == 'n' || ans == 'N'){// CHECKING FOR USR INPUT//
System.exit(0);
}
else
{
System.out.println("GIVE THE NUMBER OF ROWS");
while (!input.hasNextInt()) {
System.out.println("INPUT IS NOT VALID PLEASE PUT INTEGER VALUE:");
input.next();
}
row = input.nextInt();
while (row < 5 || row > 21) {
System.out.println("NOT VALID PUT (MINIMUM IS 5 AND MAXIMUM IS21). How many chars/last row?");
row = input.nextInt();
}
System.out.println("ENTER CHARACTER?");
value = input.next().charAt(0);
for (int i = 0; i < row; i++) { //nested for loop to create the box
System.out.print(value);
}
System.out.println();
for (int i = 0; i < row - 2; i++) {
System.out.print(value);
for (int j = 0; j < row - 2; j++) {
System.out.print(" ");
}
System.out.print(value);
System.out.println();
}
for (int i = 0; i < row; i++) {
System.out.print(value);
}
System.out.println();
System.out.println();
System.out.println("DO YOU WANT TO CONTINUE PUT Y OR N");
ans = input.next().charAt(0);
}
} while (ans== 'Y' || ans == 'y');
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.