2

Guys I want to write a program which find Nominator and Denominator with Exception Handling and calculate their result. I also added a Simple Interface which is Would you like to continue press 'y'. or 'n'. Character is in Lower Case.

I Want that Interface Occur only on two things.When Program Catch the Wrong Input. And When Result Is Calculated.

Problem is when user press 'n' it does not quit.Or When user enter any characters like 'aadswe' Interface does not again appear. I'm Stuck at this Problem.

public class Main {
        public static void main(String[] args) {
            int numeator;
            int denominator;
            double result;
            Scanner s = new Scanner(System.in);
            char m = 'y';
            do {
                try {
                System.out.print("Enter Numenator:");
                numeator = s.nextInt();

                System.out.print("Enter Denominator:");
                denominator = s.nextInt();

                result = numeator / denominator;

                System.out.println("Answer: " + result);
            } catch (InputMismatchException e) {
                System.out.println("error=> must enter integer values");
            } catch (ArithmeticException e) {
                System.out.println("error=> falseairthmtic");
            }
            System.out.println("Would you continue prees 'y' or quit press 'n'");
            m = s.next().charAt(0);
        }
        while (m == 'y');
    }
}
1
  • i entered 1,2,n respectively and the application quit without problems . what is the issue ? Commented May 25, 2015 at 8:38

1 Answer 1

2

Use following code-

public static void main(String[] args) {
            int numeator;
            int denominator;
            double result;
            Scanner s = new Scanner(System.in);
            char m = 'y';
            do {
                try {
                System.out.print("Enter Numenator:");
                numeator = s.nextInt();

                System.out.print("Enter Denominator:");
                denominator = s.nextInt();

                result = numeator / denominator;

                System.out.println("Answer: " + result);
            } catch (InputMismatchException e) {
                System.out.println("error=> must enter integer values");
            } catch (ArithmeticException e) {
                System.out.println("error=> falseairthmtic");
            }
            System.out.println("Would you continue prees 'y' or quit press 'n'");
            m = s.next().charAt(0);
            while(m!='y'&& m!='n'){
                System.out.println("you can press only 'y'or'n' "+ m+ " is not allowed!!") ;
                m = s.next().charAt(0);
            }
        }
        while (m == 'y');
            System.out.println("Has been quit");
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I appericiated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.