Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

handling an input mismatch exception

1042068Nov 27 2014 — edited Nov 28 2014

I have written the following code to calculate tax payments based on income and filing status :

import java.util.Scanner;

public class computeTax {

    public static void main(String[] args) {

       

       

   

        Scanner input = new Scanner(System.in);

        // prompt for filing status

       

        System.out.println("enter '0' for single filer,");

        System.out.println("enter '1' for married filing jointly or qualified widow(er),");

        System.out.println("enter '2' for married filing separately,");

        System.out.println("enter '3' for head of household.");

       

       

        while (true) {

       

       

        System.out.println("what is your filing status?");

       

       

         int status = input.nextInt();

       

                if (status == 0) {

                System.out.println("you're a single filer.  What is your income?");

                double income = input.nextDouble();

                //double income;

               

                   

                    if (0 <= income && income <= 8350) {

                        System.out.println("Your tax bill is : $ " + income*0.1);

                       

                    } else if (8351 <= income && income <= 33950) {

                        System.out.println("Your tax bill is : $ " +

                        ( (8350*0.1)

                        + ((income-8350)*0.15)

                        ));

                       

                    } else if (33951 <= income && income <= 68525) {

                        System.out.println("Your tax bill is : $ " +

                        ( (8350*0.1)

                        + ((33950-8350)*0.15)       

                        + ((income-33950)*0.25)

                        ));       

                               

                       

                    } else if (68526 <= income && income <= 104425) {

                        System.out.println("Your tax bill is : $ " +

                        ( (8350*0.1)

                        + ((33950-8350)*0.15)

                        + ((82250-33950)*0.25)

                        + ((income-82250)*0.28)

                        ));       

                               

                   

                    } else if (104426 <= income && income <= 186475) {

                        System.out.println("Your tax bill is : $ " +

                        ( (8350*0.1)

                        + ((33950-8350)*0.15)

                        + ((82250-33950)*0.25)

                        + ((171550-82250)*0.28)

                        + ((income-171550)*0.33)

                        ));       

                               

                       

                    } else if (186476 <= income) {

                        System.out.println("Your tax bill is : $ " +

                        ( (8350*0.1)

                        + ((33950-8350)*0.15)

                        + ((82250-33950)*0.25)

                        + ((171550-82250)*0.28)

                        + ((372950-171550)*0.33)

                        + ((income-372950)*0.35)

                        ));               

                   

                    }

                    break;

               

               

                } else if (status == 1){

                   

                    System.out.println("you're married filing jointly or qualified widow(er). What is your income?");

                    double income = input.nextDouble();

                    //double income;

                   

                    if (0 <= income && income <= 16700) {

                        System.out.println("Your tax bill is : $ " + income*0.1);

                       

                    } else if (16701 <= income && income <= 67900) {

                        System.out.println("Your tax bill is : $ " +

                        ( (16700*0.1)

                        + ((income-16700)*0.15)

                        ));

                       

                    } else if (67901 <= income && income <= 137050) {

                        System.out.println("Your tax bill is : $ " +

                        ( (16700*0.1)

                        + ((67900-16700)*0.15)       

                        + ((income-67900)*0.25)

                        ));       

                               

                       

                    } else if (137051 <= income && income <= 208850) {

                        System.out.println("Your tax bill is : $ " +

                        ( (16700*0.1)

                        + ((67900-16700)*0.15)

                        + ((137050-67900)*0.25)

                        + ((income-137050)*0.28)

                        ));       

                               

                   

                    } else if (208051 <= income && income <= 372950) {

                        System.out.println("Your tax bill is : $ " +

                        ( (16700*0.1)

                        + ((67900-16700)*0.15)

                        + ((137050-67900)*0.25)

                        + ((208850-137050)*0.28)

                        + ((income-208850)*0.33)

                        ));       

                               

                       

                    } else if (372951 <= income) {

                        System.out.println("Your tax bill is : $ " +

                        ( (16700*0.1)

                        + ((67900-16700)*0.15)

                        + ((137050-67900)*0.25)

                        + ((208850-137050)*0.28)

                        + ((372950-208850)*0.33)

                        + ((income-372950)*0.35)

                        ));               

                   

                    }

                    break;

               

                } else if (status == 2){

                   

                    System.out.println("you're married filing separately. What is your income?");

                    double income = input.nextDouble();

                    //double income;

                   

                    if (0 <= income && income <= 8350) {

                        System.out.println("Your tax bill is : $ " + income*0.1);

                       

                    } else if (8351 <= income && income <= 33950) {

                        System.out.println("Your tax bill is : $ " +

                        ( (8350*0.1)

                        + ((income-8350)*0.15)

                        ));

                       

                    } else if (33951 <= income && income <= 68525) {

                        System.out.println("Your tax bill is : $ " +

                        ( (8350*0.1)

                        + ((33950-8350)*0.15)       

                        + ((income-33950)*0.25)

                        ));       

                               

                       

                    } else if (68526 <= income && income <= 104425) {

                        System.out.println("Your tax bill is : $ " +

                        ( (8350*0.1)

                        + ((33950-8350)*0.15)

                        + ((68525-33950)*0.25)

                        + ((income-104425)*0.28)

                        ));       

                               

                   

                    } else if (104426 <= income && income <= 186475) {

                        System.out.println("Your tax bill is : $ " +

                        ( (8350*0.1)

                        + ((33950-8350)*0.15)

                        + ((68525-33950)*0.25)

                        + ((104425-68525)*0.28)

                        + ((income-104425)*0.33)

                        ));       

                               

                       

                    } else if (186476 <= income) {

                        System.out.println("Your tax bill is : $ " +

                        ( (8350*0.1)

                        + ((33950-8350)*0.15)

                        + ((68525-33950)*0.25)

                        + ((104425-68525)*0.28)

                        + ((186475-104425)*0.33)

                        + ((income-186475)*0.35)

                        ));               

                       

                        }

                   

                        break;

               

            } else if (status == 3){

               

                System.out.println("you're a head of household. What's your income?");

                double income = input.nextDouble();

               

                    if (0 <= income && income <= 11950) {

                        System.out.println("Your tax bill is : $ " + income*0.1);

                       

                    } else if (11951 <= income && income <= 45500) {

                        System.out.println("Your tax bill is : $ " +

                        ( (11950*0.1)

                        + ((income-11950)*0.15)

                        ));

                       

                    } else if (45501 <= income && income <= 117450) {

                        System.out.println("Your tax bill is : $ " +

                        ( (11950*0.1)

                        + ((45500-11950)*0.15)       

                        + ((income-45500)*0.25)

                        ));       

                               

                       

                    } else if (117451 <= income && income <= 190200) {

                        System.out.println("Your tax bill is : $ " +

                        ( (11950*0.1)

                        + ((45500-11950)*0.15)

                        + ((117450-45500)*0.25)

                        + ((income-117450)*0.28)

                        ));       

                               

                   

                    } else if (190201 <= income && income <= 372950) {

                        System.out.println("Your tax bill is : $ " +

                        ( (11950*0.1)

                        + ((45500-11950)*0.15)

                        + ((117450-45500)*0.25)

                        + ((190200-117450)*0.28)

                        + ((income-190200)*0.33)

                        ));       

                               

                       

                    } else if (372951 <= income) {

                        System.out.println("Your tax bill is : $ " +

                        ( (11950*0.1)

                        + ((45500-11950)*0.15)

                        + ((117450-45500)*0.25)

                        + ((190200-117450)*0.28)

                        + ((372950-190200)*0.33)

                        + ((income-372950)*0.35)

                        ));               

                   

                        }

               

                        break;

               

                    } else {

                       

                        System.out.println("please type the right answer");

                    }

       

               

       

           

        }

       

    }

           

}           

       

   

       

       

               

       

               

               

               

       

        

The while loop initiated on line 21 is there so that in case the wrong input is given at the prompt given in line 24, the program outputs "please type the right answer" with the command on line 254 before looping back to line 24 and prompting the user to enter his status number.  The program works as long as the input at line 28 is an integer.  Not surprisingly if the erroneous input here is not an integer, the program outputs the following error message :

Exception in thread "main" java.util.InputMismatchException

    at java.util.Scanner.throwFor(Scanner.java:909)

    at java.util.Scanner.next(Scanner.java:1530)

    at java.util.Scanner.nextInt(Scanner.java:2160)

    at java.util.Scanner.nextInt(Scanner.java:2119)

    at computeTax.main(computeTax.java:28

To try to solve this I used the Try / Catch technique with the following version of the code :

}

import java.util.Scanner;

public class computeTax {

    public static void main(String[] args) {

       

       

   

        Scanner input = new Scanner(System.in);

        // prompt for filing status

       

        System.out.println("enter '0' for single filer,");

        System.out.println("enter '1' for married filing jointly or qualified widow(er),");

        System.out.println("enter '2' for married filing separately,");

        System.out.println("enter '3' for head of household.");

       

       

        while (true) {

       

            try {

           

           

           

            System.out.println("what is your filing status?");

           

   

           

             int status = input.nextInt();

           

                    if (status == 0) {

                    System.out.println("you're a single filer.  What is your income?");

                    double income = input.nextDouble();

                    //double income;

                   

                       

                        if (0 <= income && income <= 8350) {

                            System.out.println("Your tax bill is : $ " + income*0.1);

                           

                        } else if (8351 <= income && income <= 33950) {

                            System.out.println("Your tax bill is : $ " +

                            ( (8350*0.1)

                            + ((income-8350)*0.15)

                            ));

                           

                        } else if (33951 <= income && income <= 68525) {

                            System.out.println("Your tax bill is : $ " +

                            ( (8350*0.1)

                            + ((33950-8350)*0.15)       

                            + ((income-33950)*0.25)

                            ));       

                                   

                           

                        } else if (68526 <= income && income <= 104425) {

                            System.out.println("Your tax bill is : $ " +

                            ( (8350*0.1)

                            + ((33950-8350)*0.15)

                            + ((82250-33950)*0.25)

                            + ((income-82250)*0.28)

                            ));       

                                   

                       

                        } else if (104426 <= income && income <= 186475) {

                            System.out.println("Your tax bill is : $ " +

                            ( (8350*0.1)

                            + ((33950-8350)*0.15)

                            + ((82250-33950)*0.25)

                            + ((171550-82250)*0.28)

                            + ((income-171550)*0.33)

                            ));       

                                   

                           

                        } else if (186476 <= income) {

                            System.out.println("Your tax bill is : $ " +

                            ( (8350*0.1)

                            + ((33950-8350)*0.15)

                            + ((82250-33950)*0.25)

                            + ((171550-82250)*0.28)

                            + ((372950-171550)*0.33)

                            + ((income-372950)*0.35)

                            ));               

                       

                        }

                        break;

                   

                   

                    } else if (status == 1){

                       

                        System.out.println("you're married filing jointly or qualified widow(er). What is your income?");

                        double income = input.nextDouble();

                        //double income;

                       

                        if (0 <= income && income <= 16700) {

                            System.out.println("Your tax bill is : $ " + income*0.1);

                           

                        } else if (16701 <= income && income <= 67900) {

                            System.out.println("Your tax bill is : $ " +

                            ( (16700*0.1)

                            + ((income-16700)*0.15)

                            ));

                           

                        } else if (67901 <= income && income <= 137050) {

                            System.out.println("Your tax bill is : $ " +

                            ( (16700*0.1)

                            + ((67900-16700)*0.15)       

                            + ((income-67900)*0.25)

                            ));       

                                   

                           

                        } else if (137051 <= income && income <= 208850) {

                            System.out.println("Your tax bill is : $ " +

                            ( (16700*0.1)

                            + ((67900-16700)*0.15)

                            + ((137050-67900)*0.25)

                            + ((income-137050)*0.28)

                            ));       

                                   

                       

                        } else if (208051 <= income && income <= 372950) {

                            System.out.println("Your tax bill is : $ " +

                            ( (16700*0.1)

                            + ((67900-16700)*0.15)

                            + ((137050-67900)*0.25)

                            + ((208850-137050)*0.28)

                            + ((income-208850)*0.33)

                            ));       

                                   

                           

                        } else if (372951 <= income) {

                            System.out.println("Your tax bill is : $ " +

                            ( (16700*0.1)

                            + ((67900-16700)*0.15)

                            + ((137050-67900)*0.25)

                            + ((208850-137050)*0.28)

                            + ((372950-208850)*0.33)

                            + ((income-372950)*0.35)

                            ));               

                       

                        }

                        break;

                   

                    } else if (status == 2){

                       

                        System.out.println("you're married filing separately. What is your income?");

                        double income = input.nextDouble();

                        //double income;

                       

                        if (0 <= income && income <= 8350) {

                            System.out.println("Your tax bill is : $ " + income*0.1);

                           

                        } else if (8351 <= income && income <= 33950) {

                            System.out.println("Your tax bill is : $ " +

                            ( (8350*0.1)

                            + ((income-8350)*0.15)

                            ));

                           

                        } else if (33951 <= income && income <= 68525) {

                            System.out.println("Your tax bill is : $ " +

                            ( (8350*0.1)

                            + ((33950-8350)*0.15)       

                            + ((income-33950)*0.25)

                            ));       

                                   

                           

                        } else if (68526 <= income && income <= 104425) {

                            System.out.println("Your tax bill is : $ " +

                            ( (8350*0.1)

                            + ((33950-8350)*0.15)

                            + ((68525-33950)*0.25)

                            + ((income-104425)*0.28)

                            ));       

                                   

                       

                        } else if (104426 <= income && income <= 186475) {

                            System.out.println("Your tax bill is : $ " +

                            ( (8350*0.1)

                            + ((33950-8350)*0.15)

                            + ((68525-33950)*0.25)

                            + ((104425-68525)*0.28)

                            + ((income-104425)*0.33)

                            ));       

                                   

                           

                        } else if (186476 <= income) {

                            System.out.println("Your tax bill is : $ " +

                            ( (8350*0.1)

                            + ((33950-8350)*0.15)

                            + ((68525-33950)*0.25)

                            + ((104425-68525)*0.28)

                            + ((186475-104425)*0.33)

                            + ((income-186475)*0.35)

                            ));               

                           

                            }

                       

                            break;

                   

                } else if (status == 3){

                   

                    System.out.println("you're a head of household. What's your income?");

                    double income = input.nextDouble();

                   

                        if (0 <= income && income <= 11950) {

                            System.out.println("Your tax bill is : $ " + income*0.1);

                           

                        } else if (11951 <= income && income <= 45500) {

                            System.out.println("Your tax bill is : $ " +

                            ( (11950*0.1)

                            + ((income-11950)*0.15)

                            ));

                           

                        } else if (45501 <= income && income <= 117450) {

                            System.out.println("Your tax bill is : $ " +

                            ( (11950*0.1)

                            + ((45500-11950)*0.15)       

                            + ((income-45500)*0.25)

                            ));       

                                   

                           

                        } else if (117451 <= income && income <= 190200) {

                            System.out.println("Your tax bill is : $ " +

                            ( (11950*0.1)

                            + ((45500-11950)*0.15)

                            + ((117450-45500)*0.25)

                            + ((income-117450)*0.28)

                            ));       

                                   

                       

                        } else if (190201 <= income && income <= 372950) {

                            System.out.println("Your tax bill is : $ " +

                            ( (11950*0.1)

                            + ((45500-11950)*0.15)

                            + ((117450-45500)*0.25)

                            + ((190200-117450)*0.28)

                            + ((income-190200)*0.33)

                            ));       

                                   

                           

                        } else if (372951 <= income) {

                            System.out.println("Your tax bill is : $ " +

                            ( (11950*0.1)

                            + ((45500-11950)*0.15)

                            + ((117450-45500)*0.25)

                            + ((190200-117450)*0.28)

                            + ((372950-190200)*0.33)

                            + ((income-372950)*0.35)

                            ));               

                       

                            }

                   

                            break;

                   

                        } else {

                           

                            System.out.println("please type the right answer");

                        }

       

           

                   

           

                    } catch (java.util.InputMismatchException ex){

               

                        System.out.println("Input Mismatch Exception ");

                    }       

       

        }

       

    }

           

}

I have put the try / catch structure inside the while loop.  The input error is now dealt with but when a non integer input is entered at the status prompt, the program enters an infinite loop like so  :

Input Mismatch Exception

what is your filing status?

Input Mismatch Exception

what is your filing status?

Input Mismatch Exception

what is your filing status?

Input Mismatch Exception

etc

etc

etc...

The problem here is that the program no longer stops at the status prompt on line 27.

Can anyone suggest a way of overriding the input mismatch error thing and looping back to a status prompt that actually stops and waits for the input?

Thanks

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 26 2014
Added on Nov 27 2014
5 comments
2,664 views