0

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;

    }

}
lczapski
  • 3,644
  • 3
  • 10
  • 26
zpgreeny
  • 5
  • 1
  • 1
    The `try` should include `keyboard.nextInt()`, and the `catch` should include `keyboard.next()` (to consume whatever wasn't an `int`). – Elliott Frisch Nov 23 '19 at 18:49
  • [How to handle infinite loop caused by invalid input (InputMismatchException) using Scanner](https://stackoverflow.com/q/3572160), [How to use Scanner to accept only valid int as input](https://stackoverflow.com/q/2912817), [Validating input using java.util.Scanner](https://stackoverflow.com/q/3059333) – Pshemo Nov 23 '19 at 18:50

1 Answers1

1

There are a few things you might want to do in your code:

  1. Move the main method to an empty Runner class of its own. All it does is instantiate and run Assignment2.
  2. Make sure your fields are the first thing you define in your class. It makes it easier to read your code.
  3. Generally, static fields are written in all caps. That's just a convention of course. You'll find lots of style guides online e.g. Google's guide.
  4. Understanding your error.

I ran your code and typed in a String (java). The result was:

Enter a number between (2-15): 
java
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at eu.webfarmr.Assignment2.numRowCol(Assignment2.java:35)
    at eu.webfarmr.Assignment2.playWordSearch(Assignment2.java:26)
    at eu.webfarmr.Assignment2.main(Assignment2.java:15)

The stack trace actually tells you where the error occurred (Assignment2.java:35). So you know that this is the part of the code you need to wrap in a try/catch.

For instance you could do:

public int[][] numRowCol() {
    int array[] = new int[2];

    try {
        for (int i = 0; i <= 1; i++) {
            System.out.printf("Enter a number between (2-15): %n");
            array[i] = keyboard.nextInt();
        }
        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());
    } catch (Exception err) {
        System.out.println("Another error occurred.");
        err.printStackTrace();
    }
    int arrayArray[][] = new int[array[0]][array[0]];
    return arrayArray;
}

But maybe you should just read the input as string and then try to convert the input into an integer / double / other. That would give you more control over the conversion process.

Also, you want to think about what it means to have a failure. In your case, you'd end up with a partially initialized array of values.

David Brossard
  • 12,223
  • 6
  • 42
  • 72