Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The Program which i created to catch the input error.Catch works Fine but when i use do while to say user if you want to continue press 'y' or finish this press 'n' but when user enter any character expect 'n' it continue the same.when i debug this debugger ignore character input.
This is the Code.
Java
import java.util.Scanner;
import java.util.InputMismatchException;
public class Asd {

    public static void main(String[] args) {
        int numeator;
        int denominator;
        double result;
        Scanner s = new Scanner(System.in);
          
        char m;
        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("If yes prees 'y' or no 'n'");
            m=s.next().charAt(0);
        } while(m!='n');
    
    }
}

I want to fix this problem when user press 'y' then it continue the same procedure.At any other characters it give same message.or'n' for exit
Posted
Updated 24-May-15 2:39am
v3

1 solution

You do not check to see if the user pressed 'y', only if he/she did not press 'n'. So any character other than 'n' will continue the loop. You need to add another loop to check which character is pressed, something like:
Java
    do {
        System.out.println("If yes press 'y' or no 'n'");
        m=s.next().charAt(0);
        if (m == 'y' || m == 'n')
            break;
    } while (1);
} while(m == 'y');
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900