import java.util.Scanner;
public class Lab4_5 {
public static void main(String[]args) {
Scanner scan= new Scanner(System.in);
int rows=0;
int rowIndex=0, colIndex=0;
boolean choice1= true;
String y="y";
String n="n";
boolean first = true;
while (choice1==true) {
if (first==true) {
first=false;
System.out.println("Do you want to start(Y/N): ");
} else if (first==false) {
System.out.println("Do you want to continue(Y/N): ");
}
String choice2=scan.next();
if (choice2.equals(y)) {
System.out.println("How many rows/columns(5-21)?");
rows=scan.nextInt();
while (rows<5 || rows>21) {
System.out.println("That is either out of range or not an integer, try again! ");
rows=scan.nextInt();
}
System.out.println("What character?");
String choice3=scan.next();
System.out.println(" ");
for (rowIndex=1; rowIndex<=rows; rowIndex++) {
for (colIndex=1; colIndex<=rows; colIndex++) {
if (rowIndex==1 || rowIndex==rows || colIndex==1 || colIndex==rows) {
System.out.print(choice3);
} else {
System.out.print(" ");
}
}
System.out.println();
}
} else if(choice2.equals(n)) {
choice1 = false;
System.out.println("Thank you. Goodbye.");
} else {
System.out.println("Please either enter Y or N.");
}
}
}//end of main
}
The code prints what I need it to print, but I also have to have something in the code when it asks how many rows/columns to catch whether or not i input something other than an integer(in the part below). need some help, we haven't done anything yet with how to catch exceptions and i don't know how to start.
String choice2=scan.next();
if (choice2.equals(y)) {
System.out.println("How many rows/columns(5-21)?");
rows=scan.nextInt();
while (rows<5 || rows>21) {
System.out.println("That is either out of range or not an integer, try again! ");
rows=scan.nextInt();
}
}