3

I'm still in the learning part of Java. I've made a kind of guessing game. It looks like this:

import java.util.Scanner;
import java.util.Random;
public class guessing_game {

    static Scanner input = new Scanner(System.in);
    static Random generator = new Random();
    public static void main(String[] args) {
        int number;
        number = generator.nextInt(20);
        System.out.println("Guess the number!");
        game(number);
    }
    public static void game(int number) {
        int inputStorage;
        inputStorage = input.nextInt();
        if (inputStorage == number) {
            System.out.println("You've guessed the right number!");
        }
        else if (inputStorage != number) {
            System.out.println("Wrong number, try again!");
            game(number);
        }
    }
}

Now I have a problem. My little sister and I played this "game". My sister was typing on the numpad. She accidently hit the + button before pressing enter and I got some errors. My question is: How can I let my application print a line which is saying that you can only input numbers and then restarts the game stub again?

4 Answers 4

4

One way would be to wrap the input.nextInt() in a try catch statement and catch the exceptions that are thrown by input.nextInt(), InputMismatchException. A good tutorial for try catch statements is here if you aren't sure what I am talking about.

try {
    inputStorage = input.nextInt();
} catch (InputMismatchException e){
    System.out.println("invalid type");
}

Another way you can do this is:

if(input.hasNextInt()){
     inputStorage = input.nextInt();
 }else{
      System.out.println("invalid type");
 }

There is also an error with continuing the game try using a while loop with a break if the number was guessed correctly:

int inputStorage;
boolean notGuessed = true;
while(notGuessed)
{
    if(input.hasNextInt()){
         inputStorage = input.nextInt();
     } else{
         System.out.println("invalid type");
      }
    if (inputStorage == number) {
        System.out.println("You've guessed the right number!");
        notGuessed = false;
    }
    else if (inputStorage != number) {
        System.out.println("Wrong number, try again!");

    }
 }
Sign up to request clarification or add additional context in comments.

7 Comments

I'm a real beginner in Java, is it possible to change the not guessed boolean to guessed and set it to false by default? I'm not on my pc right now so I can't test it...
Yes this is possible, but you would have to add a logical or to the while condition: while(!guessed).
It doesn't work. When I enter a letter the "invalid type" text comes up and doesn't stop printing. It keeps going and going...
It's not supposed to stop until the number is correctly guessed. If you want a different condition then change the loop structure.
I made it print the invalid type text only once but I don't understand why it's working by now. pastebin.com/8m4WsV0T
|
1

Well this is quite easy. You can accomplish it in various way.

Try this one

    public static int checkInt(String strNumber) {
    int Number;
    try {           
        Number = Integer.parseInt(strNumber);
    } catch (NumberFormatException ex) {
        Number = -1;
    }
    return Number;
}

Or even simpler:

    public static int checkInt(String strNumber) {

    Number = Integer.parseInt(strNumber, -1);

    return Number;
}

The second one is even simpler because you omit a try catch block, that is rather not correctly used in such case. Read about the functions of Integer class.

Comments

0

You can use a try/catch:

boolean b = true;
while (b) {
    try {
          inputStorage = input.nextInt();
          b= false;
    } catch (InputMismatchException e) {
          System.out.println("Invalid input.  Please enter again!");
    } 
}

Comments

0

Since the error you got was an Exceptiion: InputMismatchException.

You can explicitly handle the exception using the Exception handling mechanism in java.

Read this

Read this

to know how it actually works.

Above suggested answers are exception handling only.

Comments

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.