0

I am just starting to write a blackjack game with java. I am trying to get the program to ask the user to enter again if the cash they typed in to start with isn't a valid integer. I see many examples of the try statement with catch, but none of them is working. The program gives the error InputMismatchException cannot be resolved to a type. A thread I have followed is this one, and I have the exact same code, just different variable name. Here it is. Java InputMismatchException

Here is my code:

        Scanner input_var=new Scanner(System.in);
        System.out.println("Welcome to BlackJack!");
        System.out.println("Enter how much money you will start with");
        System.out.println("Starting cash must be whole number");
        int money=0;
        do{

        try{
            System.out.println("Enter how much money you will start with: ");
            money=input_var.nextInt();
            }

        catch (InputMismatchException e){
            System.out.println("Sry, not a valid integer");
            input_var.nextInt();
        }
        input_var.nextLine();
        }while (money<=0);

Any help with why my almost exact code isn't working would be greatly appreciated. Thanks for your time and effort.

Community
  • 1
  • 1
Bob G.
  • 143
  • 3
  • 14
  • 1
    Did you remember to import the exception? "import java.util.InputMismatchException" – E.D. May 18 '17 at 18:41
  • I did just do that now and it still doesn't work. It does print "sry not valid integer" but doesn't give user to enter a number in again. What is wrong? – Bob G. May 18 '17 at 18:44
  • 1
    It's not a good idea to use exceptions as business logic. They should be for exceptional situations only. Bad input doesn't qualify. It's a newbie mistake to be so focused on user input this way. Why not start with command line arguments and get your game working? Add the interactions later. – duffymo May 18 '17 at 18:45
  • it's doing what your code says to do now. After it prints sorry, it waits for you to enter an int. – E.D. May 18 '17 at 18:47
  • Also, this next request for an int is not contained in a try, so invalid input would break your program. – E.D. May 18 '17 at 18:48
  • It gets terminated after saying sry not a valid integer, so user cannot put anything in. – Bob G. May 18 '17 at 18:49
  • @Umar Farooq answer is correct. You should not use Exceptions when you can use other code to make the same decision. test with hasNextInt() followed by next() and the error message when it's false OR nextInt() when true – Novaterata May 18 '17 at 18:52

4 Answers4

2

Consume using input.next(), as input.nextInt() doesn't exist. That's the whole point of the Exception.

do{
    try{
        System.out.println("Enter how much money you will start with: ");
        money=input_var.nextInt();
    }catch (InputMismatchException e){
        System.out.println("Sry, not a valid integer");
        input_var.next();
    }
}while (money<=0);
dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
1

You can use hasNextInt() within a while loop. While the next input is not an integer, display the "not a valid integer" message and get the next input. When it is an integer, the while loop will break and you can do what you need to do.

Umar Farooq
  • 321
  • 1
  • 9
1

Remove the input_var.nextInt(); from the try statement and input_var.nextLine();. There has to be an import like this import java.util.* or import java..util.InputMismatchException.

Which IDE are you using?

1

Maybe you're looking for NumberFormatExcpetion? It's what gets thrown when converting strings to numbers. Try doing this instead:

    Scanner input_var=new Scanner(System.in);
    System.out.println("Welcome to BlackJack!");
    System.out.println("Enter how much money you will start with");
    System.out.println("Starting cash must be whole number");
    int money=0;

    do {
        try {
            System.out.println("Enter how much money you will start with: ");
            money = Integer.parseInt(input_var.next());

        }
        catch(NumberFormatException e) {
            System.out.println("Sry, not a valid integer");
            input_var.next();
        }

        input_var.next();

    } while (money<=0);
nixin72
  • 322
  • 4
  • 16
  • I am looking for if user enters something like 6.7, it says sry not valid and they have to enter something like 7, which is whole. I am not looking for a user entering like f6 string error. – Bob G. May 18 '17 at 18:52
  • Yes, my code handles that. It's making a call to `Integer.parseInt()`, which will throw and error if the user enters a floating point number. It's then unable to be parse to an integer as opposed to a double, and the `NumberFormatExcpetion` is thrown. – nixin72 May 18 '17 at 18:57