0

I wrote this little guessing game where you have to guess of cause a random number. I finished this programm but the last thing I would like to to do is: When the user should input a number and its and InputMissmatching error or other errors, the System tells him that he has to input a number taller than 0. But the variable of the Scanner doesn't work after this do-while loop anymore! How to I have to set up the try/catch/finally with a loop to get only integers and not crash when its not an integer or 0?

The problem is that the guessNumber and the numberOfUser variables havent't been initialized now! lines: 37,53 so what do i have to change in the try/catch loop?

package gessinggame;

import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

public class GessingGame {

    public static void main(String[] args) {




//<editor-fold desc="Start Variables and Objects">

        Random random = new Random();
        Scanner input = new Scanner(System.in);

        int counter = 0;
        int numberOfUser;
        int guessNumber;
        int x = 1;
        System.out.print("Try to guess my number! What should be the highest number I can go up to?: ");
        do{
        try{
            numberOfUser = input.nextInt();
            guessNumber = random.nextInt(numberOfUser);
            x = 2;
        }catch(Exception e)
        {
            System.out.print("That doesn't work! Please enter an integer higher than 0 :");
            x = 1;
        }
        }while(x == 1);
//</editor-fold>

//<editor-fold desc="do-while loop (higher/lower/equal)">

        System.out.println("Try to guess my number! Its from 0 to " + numberOfUser + " !");
        System.out.println("---------------------------------------------------");

        do{

                int userguess = input.nextInt();

                    if(userguess > numberOfUser)
                    {
                    System.out.println("Oh man, at the beginning you said that the highest number is: " + numberOfUser);
                    }
                    else{       
                            if(userguess < 0)
                            {
                            System.out.println("My number is always taller than 0");
                            } else{  
                                    if(guessNumber < userguess)
                                    {
                                        System.out.println("lower");
                                        counter++;
                                    }
                                    if(guessNumber > userguess) 
                                    {
                                        System.out.println("higher");
                                        counter++;
                                    }
                                    if(userguess == guessNumber) 
                                    {
                                        counter++;
                                        break;
                                    }
                                    }
                        }
            }while(true);

//</editor-fold>

//<editor-fold desc="print final result">

        System.out.println("You guessed my number! My number was: " + guessNumber + "! You needed " + counter + " attempts!");

//</editor-fold>
    }

}
Mindmax
  • 49
  • 1
  • 7

2 Answers2

0

So the problem is that first you need to catch the exception of course. In the try catch you have to clear the buffer then with input.nextLine();

So this should work:

public static void main(String[] args)
{

    // <editor-fold desc="Start Variables and Objects">

    Random random = new Random();
    Scanner input = new Scanner(System.in);

    int counter = 0;
    int numberOfUser = 0;
    int guessNumber = 0;
    int x = 1;
    System.out.print("Try to guess my number! What should be the highest number I can go up to?: ");
    do
    {
        try
        {
            numberOfUser = input.nextInt();
            guessNumber = random.nextInt(numberOfUser);
            x = 2;
        }
        catch (Exception e)
        {
            System.out.print("That doesn't work! Please enter an integer higher than 0 :");
            x = 1;
        }
    } while (x == 1);
    // </editor-fold>

    // <editor-fold desc="do-while loop (higher/lower/equal)">

    System.out.println("Try to guess my number! Its from 0 to " + numberOfUser + " !");
    System.out.println("---------------------------------------------------");

    do
    {

        int userguess = 0;
        try
        {
            userguess = input.nextInt();
        }
        catch (InputMismatchException e)
        {
            System.out.println("Please enter a valid number!");
            input.nextLine();
        }

        if (userguess > numberOfUser)
        {
            System.out.println("Oh man, at the beginning you said that the highest number is: " + numberOfUser);
        }
        else
        {
            if (userguess < 0)
            {
                System.out.println("My number is always taller than 0");
            }
            else
            {
                if (guessNumber < userguess)
                {
                    System.out.println("lower");
                    counter++;
                }
                if (guessNumber > userguess)
                {
                    System.out.println("higher");
                    counter++;
                }
                if (userguess == guessNumber)
                {
                    counter++;
                    break;
                }
            }
        }
    } while (true);

    // </editor-fold>

    // <editor-fold desc="print final result">

    System.out.println(
            "You guessed my number! My number was: " + guessNumber + "! You needed " + counter + " attempts!");

    // </editor-fold>
}
bbrinck
  • 953
  • 1
  • 9
  • 33
  • Thanks, but its still not working, because the values don't work properly in the try/case thing... @billbrinck – Mindmax Nov 29 '19 at 11:08
0

Add input.nextLine() in your try/catch blocks.
call Scanner.nextLine call after each Scanner.nextInt to consume rest of that line including newline.

 package gessinggame;

import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

public class GessingGame {

    public static void main(String[] args) {




//<editor-fold desc="Start Variables and Objects">

        Random random = new Random();
        Scanner input = new Scanner(System.in);

        int counter = 0;
        int numberOfUser = 0;
        int guessNumber = 0;
        int x = 1;
        System.out.print("Try to guess my number! What should be the highest number I can go up to?: ");
        do{
        try{
            numberOfUser = input.nextInt();
            guessNumber = random.nextInt(numberOfUser);
            x = 2;
        }catch(Exception e)
        {
            System.out.print("That doesn't work! Please enter an integer higher than 0 :");
            input.nextLine();
            x = 1;
        }
        }while(x == 1);


        System.out.println("Try to guess my number! Its from 0 to " + numberOfUser + " !");
        System.out.println("---------------------------------------------------");

        do{

                int userguess = input.nextInt();

                    if(userguess > numberOfUser)
                    {
                    System.out.println("Oh man, at the beginning you said that the highest number is: " + numberOfUser);
                    }
                    else{       
                            if(userguess < 0)
                            {
                            System.out.println("My number is always taller than 0");
                            } else{  
                                    if(guessNumber < userguess)
                                    {
                                        System.out.println("lower");
                                        counter++;
                                    }
                                    if(guessNumber > userguess) 
                                    {
                                        System.out.println("higher");
                                        counter++;
                                    }
                                    if(userguess == guessNumber) 
                                    {
                                        counter++;
                                        break;
                                    }
                                    }
                        }
            }while(true);
        System.out.println("You guessed my number! My number was: " + guessNumber + "! You needed " + counter + " attempts!");
    }

}
Somil Garg
  • 474
  • 4
  • 12
  • Thanks, but it still doesn't work... the error is alway in the variables numberOfUser and guessNumber after this try/catch loop... so the variables don't get initialized in these cases! – Mindmax Nov 29 '19 at 12:33
  • @Mindmax i have updated the code with initialized variables . It is working for me. – Somil Garg Nov 29 '19 at 12:45
  • Lol now it works? did you change the code again? alright i will now find out why but thx haha. EDIT: OH LORD You initalized the vars at the beginning? Alright there we go. @Somil Garg – Mindmax Nov 29 '19 at 13:04
  • Do you know why you have to do this at the beginning? – Mindmax Nov 29 '19 at 13:07
  • Because if the variables are not getting initialised inside the try block due to some exception they will remain uninitialised throughout the program so you will have to do that beforehand. – Somil Garg Nov 29 '19 at 13:09
  • https://stackoverflow.com/q/1560685/4007703 Read this for more info. – Somil Garg Nov 29 '19 at 13:13
  • Yeah but the do-while loop causes, that the variables don't go uninitialised throughout the programm? – Mindmax Nov 29 '19 at 14:22