简体   繁体   中英

In the java programming, if the input validation fails, how can I allow user to retry three times before throwing an exception and terminating?

In the java programming, if the input validation fails, how can I allow user to retry three times before throwing an exception and terminating?

Sample Run
Enter taxable income ... 80
The taxable income must be at least $1200.0
Enter taxable income ... 890
The taxable income must be at least $1200.0
Enter taxable income ... 1090
The taxable income must be at least $1200.0
Exception in thread "main" java.lang.RuntimeException: Sorry
you're having trouble

Use loop in which

  • read and validate input from user.
  • if validation will work fine exit loop
  • if validation will fail
    • increase tries counter
    • if tries counter will be greater than predefined max throw runtime exception with your message.

Thanks evryone. @pshemo you are right, it is a homework assignment and so I have to compulsory use the "ToolBox.crash" method to terminate the program if the input validation fails after the third try. I solved the issue according to your suggestion. I initialized a loop counter inside the loop. Here is the complete code of the program which calculates the income tax of a person.

import java.util.Scanner;
import java.io.PrintStream;
import type.lib.ToolBox;

public class L5B
{

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        PrintStream output = System.out;

        final double MIN_AMOUNT = 1200.0;
        final double BASIC_AMOUNT = 43561.0;
        final double LEVEL_ONE = 43561.0 + 43562.0;
        final double LEVEL_TWO = LEVEL_ONE + 47931.0;
        final double TWENTY_ONE = 21/100.0;
        final double THIRTY_THREE = 33/100.0;
        final double THIRTY_EIGHT = 38/100.0;
        final double HUNDRED = 100.0;
        final double FORTY_TWO = 42/100.0;
        final int THREE = 3;
        final int TWO = 2;


        output.print("Enter taxable income ... ");
        double income;
        int i = 0;
        for(income = input.nextDouble(); income < MIN_AMOUNT; )
        {

            output.println("The taxable income must be at least " + MIN_AMOUNT);
            output.print("Enter taxable income ... ");
            income = input.nextDouble();
            i++;

                if(i >= TWO)
                {
                        ToolBox.crash(true,"Sorry you're having trouble.");
                }           
        }


        if (income <= BASIC_AMOUNT)
        {
            double totalTax = income * TWENTY_ONE;
            double averageRate = TWENTY_ONE * HUNDRED;
            double marginalRate = averageRate;
            output.printf("Tax = %,.2f", totalTax);
            output.printf("\nAverage Rate = %.1f%%", averageRate);
            output.printf("\nMarginal Rate = %.1f%%", marginalRate);

        }
        else if (income > BASIC_AMOUNT && income <= LEVEL_ONE)
        {
            double totalTax = BASIC_AMOUNT * TWENTY_ONE + ((income - BASIC_AMOUNT)*THIRTY_THREE);
            double averageRate = HUNDRED*totalTax/income;
            double marginalRate = THIRTY_THREE*HUNDRED;
            output.printf("Tax = %,.2f", totalTax);
            output.printf("\nAverage Rate = %.1f%%", averageRate);
            output.printf("\nMarginal Rate = %.1f%%", marginalRate);
        }
        else if (income > LEVEL_ONE && income <= LEVEL_TWO)
        {
            double totalTax = BASIC_AMOUNT * TWENTY_ONE + (LEVEL_ONE - BASIC_AMOUNT) * THIRTY_THREE + ((income - LEVEL_ONE) * THIRTY_EIGHT);
            double averageRate = totalTax*HUNDRED/income;
            double marginalRate = THIRTY_EIGHT*HUNDRED;
            output.printf("Tax = %,.2f", totalTax);
            output.printf("\nAverage Rate = %.1f%%", averageRate);
            output.printf("\nMarginal Rate = %.1f%%", marginalRate);
        }
        else if (income > LEVEL_TWO)
        {
            double totalTax = BASIC_AMOUNT * TWENTY_ONE + ((LEVEL_ONE - BASIC_AMOUNT) * THIRTY_THREE) + ((LEVEL_TWO - LEVEL_ONE) * THIRTY_EIGHT) + ((income - LEVEL_TWO) * FORTY_TWO);
            double averageRate = totalTax*HUNDRED/income;
            double marginalRate = FORTY_TWO * HUNDRED;
            output.printf("Tax = %,.2f", totalTax);
            output.printf("\nAverage Rate = %.1f%%", averageRate);
            output.printf("\nMarginal Rate = %.1f%%", marginalRate);
        }



    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM