here is my code: I want to enter ints, and if I input a string, i get an exception, however my try catch doesn't work. I just need help to gracefully handle input. Thanks. I am working on this for a college assignment, and quite frankly I have no idea how to do this, since the examples he has provided in class don't neccessarily work the same.
public static void main (String[]args) {
Assignment2 game = new Assignment2();
game.playWordSearch();
// System.out.println(game.numRowCol());
//System.out.printf("The words to find: %n %s%n %s%n %s%n %s%n %s%n %n", game.wordStorage(), game.wordStorage(), game.wordStorage(), game.wordStorage(), game.wordStorage());
}
Scanner keyboard = new Scanner(System.in);
private int[][] wordBoard;
private String[] wordList;
private static int row;
private static int col;
public void playWordSearch(){
wordBoard = numRowCol();
wordStorage();
}
public int[][] numRowCol(){
int array[] = new int[2];
for(int i=0; i<=1; i++) {
System.out.printf("Enter a number between (2-15): %n");
array[i] = keyboard.nextInt();
}
try {
System.out.println(array[0]);
System.out.println(array[1]);
} catch (
java.util.InputMismatchException mismatchException) {
System.out.printf("That created an exception, the message is %s%n", mismatchException.getMessage());
}
int arrayArray [][] = new int[array[0]][array[0]];
return arrayArray;
}
public String wordStorage(){
Scanner keyboard = new Scanner(System.in);
System.out.printf("Enter a word with less than 8 characters: ");
String word = keyboard.nextLine();
return word;
}
}