Home > Blockchain >  Why is my While Loop wont loop my Catch, like my if else statement?
Why is my While Loop wont loop my Catch, like my if else statement?

Time:11-08

I am trying to make a program where the user will input a random integer/number. But the main issue is, if user accidentally input a String, I want them to be informed and also I want them to ask to input again by asking them again. But in my code I cant loop my catch.

public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        int answer = 28;
        int attempts = 0;
        
        System.out.println("Guess a Number 1 - 50:");
        
        
        while(true) {
            try {
                int input = scan.nextInt();
                scan.nextLine();
                if(input > answer) {
                    System.out.println("Too Big");
                    System.out.println("Guess a Number 1 - 50:");
                } else if (input < answer) {
                    System.out.println("Too Small");
                    System.out.println("Guess a Number 1 - 50:");
                } else if (input == answer ) {
                    System.out.println("Congrats!");
                } 
            } catch (InputMismatchException e) {
                System.out.println("Numbers only");
                System.out.println("Guess a Number 1 - 50:");
                
            } 
            
            
        }

}
}

CodePudding user response:

Try-catch block stops the execution of the while loop when it throws an error (when you had a catch). You can use another if-else statement to check if it was a number instead of a try-catch block.

public static boolean isNumeric(String string) {
    int intValue;
        
    System.out.println(String.format("Parsing string: \"%s\"", string));
        
    if(string == null || string.equals("")) {
        System.out.println("String cannot be parsed, it is null or empty.");
        return false;
    }
    
    try {
        intValue = Integer.parseInt(string);
        return true;
    } catch (NumberFormatException e) {
        System.out.println("Input String cannot be parsed to Integer.");
    }
    return false;
}

you can use a helper method like this to return true or false in your if statement.

check this URL for more information.

ps : This is only one method of implementation. I think you will be able to work around with some solution with try-catch also. You can try implementing inputting user input again inside the catch block to repeat the process.

CodePudding user response:

You should try this code, It will work perfectly for your requirement...


    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int answer = 28;
        int attempts = 0;
        System.out.println("Guess a Number 1 - 50:");

        boolean condition = true;

        while (condition) {
            try {
                boolean condition2=true;
                while (condition2) {
                    int input = scan.nextInt();
                    scan.nextLine();
                    if (input > answer) {
                        System.out.println("Too Big");
                        System.out.println("Guess a Number 1 - 50:");
                    } else if (input < answer) {
                        System.out.println("Too Small");
                        System.out.println("Guess a Number 1 - 50:");
                    } else {
                        System.out.println("Congrats!");
                        condition2=false;
                    }
                }
                condition = false;
            } catch (InputMismatchException e) {
                System.out.println("Numbers only");
                System.out.println("Guess a Number 1 - 50:");

            }


        }
    }

  •  Tags:  
  • java
  • Related