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.

7
  • 1
    Did you remember to import the exception? "import java.util.InputMismatchException" Commented May 18, 2017 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? Commented May 18, 2017 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. Commented May 18, 2017 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. Commented May 18, 2017 at 18:47
  • Also, this next request for an int is not contained in a try, so invalid input would break your program. Commented May 18, 2017 at 18:48

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Still gives same error of Exception in thread "main" java.util.InputMismatchException.
@BobG. Are you doing exactly as above ? Post your updated code.
Now it works thanks to you. The .next I must have missed.
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.

Comments

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 Comment

I am using eclipse.
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);

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.