Home > OS >  My "Error" Handler in a While Loop Java doesn't read
My "Error" Handler in a While Loop Java doesn't read

Time:11-25

My code is working, but the problem is that the error handler, the if statement doesn't read. when the user inputted that does not in the choices of Y/N, for example 'g'.

Enter expression: a b

Try Again? (Y/N) : g

Enter expression:

_

I WANT TO BE THE RESULT IS:

Enter expression: a b

Try Again? (Y/N) : g

Error, Please Try Again!

Try Again? (Y/N) :

_

My error handler doesn't read


    if (choice.equals("1")) {
                String YN ="Y";
                while (!YN.equalsIgnoreCase("N")) {
                    System.out.print('\u000C');
                    System.out.print("Enter the Infix Expression: ");
                    String exp = BReader.readLine();
                    try {
                        System.out.println("Postfix Expression: "   infixToPostfix(exp));
                    } catch(Exception e) {
                        System.out.println("Enter correct expression.");
                    }
                    System.out.println();
                    System.out.println("Try Again? (Y/N) : ");
                    YN = BReader.readLine();
                    if(!YN.equalsIgnoreCase("N") || !YN.equalsIgnoreCase("Y")){
                    System.out.print("Error, Please Try Again!");
                    }else{
                    }
                }

CodePudding user response:

You need to loop until you get the right answer, as below. Also, it should be a && join:

        while(!YN.equalsIgnoreCase("N") && !YN.equalsIgnoreCase("Y")){
           System.out.println("Error, Please Try Again!");
           System.out.println("Try Again? (Y/N) : ");
           YN = br.readLine();
        }
  • Related