0

Guessing the number

import java.util.*;

public class LabExer5A
{
    public static void main(String args[])
    {       
        Scanner Input = new Scanner (System.in); 
        System.out.println("Guess a number between 1 and 50");
        int JPS_Guess = Input.nextInt();
        int JPS_CorrectNum = 25;
        int JPS_NextGuess;  
    do
    {
    try
    {
            if (JPS_Guess == JPS_CorrectNum)
            {
                System.out.println("You got it in ");
            }
            else if (JPS_Guess < JPS_CorrectNum && JPS_Guess >= 1)
            {
                System.out.println("Too low. Try again.");
                JPS_NextGuess = Input.nextInt();
                JPS_Guess = JPS_NextGuess;
            }
            else if (JPS_Guess > JPS_CorrectNum && JPS_Guess <= 50)
            {
                System.out.println("Too High. Try again.");
                JPS_NextGuess = Input.nextInt();
                JPS_Guess = JPS_NextGuess;
            }
            else if(JPS_Guess < 1 || JPS_Guess > 50)
            {
                throw new InputMismatchException();
            }
    }
         catch(InputMismatchException e) 
         {
            System.out.println("Invalid value. Please enter a number between 1 and 50: ");
            JPS_NextGuess = Input.nextInt();
            JPS_Guess = JPS_NextGuess;
         }   
          catch (NumberFormatException ex) 
         {
            System.out.println("Error - Enter Numerical Values Only");
            JPS_NextGuess = Input.nextInt();
            JPS_Guess = JPS_NextGuess;
         }  
    }
    while (JPS_Guess != JPS_CorrectNum);//will rerun the program until the user guess the correct number   
}
}


}

}

I'm not sure where I made the mistake. Either the use of the throw catch or maybe because of the way I rerun the program. I'm pretty new to using exception and try-catch Please help and be kind. Thank you.

Wet_Pantz
  • 237
  • 1
  • 5
  • 19

1 Answers1

0

nextInt() actually throws InputMismatchException when you input other data type so you can directly use the catch for InputMismatchException and you should add Input.next() so that the scanner will take in the non integer input

Another thing is that Input.nextInt() should always be inside the try/catch. In your code, the exception will not be catched for the first input and after you input any wrong value.

 catch(InputMismatchException e) 
         {
            System.out.println("Invalid value. Please enter a number between 1 and 50: ");
            JPS_NextGuess = Input.nextInt();
            JPS_Guess = JPS_NextGuess;
         }   

I have modified your code to be simpler. I suggest you use boolean to keep track of whether the number is guessed so you don't have to interchange the guess. Also, good indentation formatting helps reading.

import java.util.*;

public class LabExer5A
{
    public static void main(String args[])
    { 
        Scanner Input = new Scanner (System.in); 
        System.out.println("Guess a number between 1 and 50");
        int JPS_CorrectNum=25;
        boolean isGuessed=false;  
        do
        {
            try
            {
                int JPS_Guess = Input.nextInt();
                if (JPS_Guess == JPS_CorrectNum)
                {
                    System.out.println("You got it in ");
                    isGuessed=true;
                }
                else if (JPS_Guess < JPS_CorrectNum && JPS_Guess >= 1)
                {
                    System.out.println("Too low. Try again.");
                }
                else if (JPS_Guess > JPS_CorrectNum && JPS_Guess <= 50)
                {
                    System.out.println("Too High. Try again.");
                }
                else if(JPS_Guess < 1 || JPS_Guess > 50)
                {
                    throw new InputMismatchException();
                }
            }
            catch(InputMismatchException e) 
            {
                System.out.println("Invalid value. Please enter a number between 1 and 50: ");
                Input.next();
            }   
        }
        while (!isGuessed);//will rerun the program until the user guess the correct number   
    }
}
buzzbuzz
  • 209
  • 1
  • 3