(应该)简单的双重验证

时间:2013-04-29 09:58:17

标签: java double

我很难确保在我的程序中正确验证双打。用户可以输入存入账户的金额,这应该是一个双倍(我知道,这不是我应该使用的,但它是分配指南的一部分)。从理论上讲,用户应该可以存入任何金额 - 不仅仅是30英镑,而是15.23英镑。 这是我目前的验证,它允许数字,但阻止输入句号,这会产生许多问题。

这是我到目前为止的代码:

public static String getBalanceValidation()
{
    //Allow user input capabilities
    Scanner input = new Scanner (System.in);
    //Declare variables needed for validation
    double dblInput = 0; //dblInput is set as 0
    String strNumber = ""; //strNumber is blank
    boolean bolSuccessful, bolNumeric;
    int intCount;
    char charLetter;


    do
    {
        //set bolSuccessful and bolNumeric as true
        bolSuccessful = true;
        bolNumeric = true;

        try //try user input
            {
                System.out.println("Enter the balance to be deposited: "); //User prompt
                strNumber = input.next(); //User input as string
                dblInput = Double.parseDouble(strNumber) ; //String input converted to double


            }// end of try

        catch (NumberFormatException e) //NumberFormatException disallows letters or symbols in value
            {
                System.out.println("Deposit value cannot contain letters!"); //Error message
                bolSuccessful = false; //set bolSuccessful as false

                continue; //Return to try
            }//end of number format catch


            //create for loop which checks each character throughout the string
            for (intCount = 0; intCount < strNumber.length(); intCount++)
                {
                    charLetter = strNumber.charAt(intCount); //charLetter is the alphanumeric value of a character in the string at the point dictated by intCount


                    if (!(charLetter >= '0') && (charLetter <= '9' ) //if charLetter is not between 0 and 9
                            || (charLetter == '.')) //or charLetter is not a full stop
                        {
                            bolNumeric = false; //Set bolNumeric as false
                        }//end of if construct
                }//end of for loop

            if (!bolNumeric) //if bolNumeric is false
                {
                    System.out.println("Incorrect input format! The balance must be numbers only!"); //Error message
                    bolSuccessful = false; //Set bolSuccessful as false
                }//end of if construct

    }while (!bolSuccessful); //While bolSuccessful is false, return to top


    return strNumber; //return strNumber to be used in main method
    //end of do method
}//end of getBalanceValidation method

我不确定是不是因为我使用了NumberFormatException(还有其他什么用于双倍?)

非常感谢

3 个答案:

答案 0 :(得分:0)

您的布尔表达式中有 2个错误

if (!(charLetter >= '0') && (charLetter <= '9' ) || (charLetter == '.')) 

此条件相当于:

if ((charLetter < '0') && (charLetter <= '9' ) || (charLetter == '.')) 

可以简化为:

if ((charLetter < '0') || (charLetter == '.')) 

因此 !应该应用于表达式的前两部分

if (!( (charLetter >= '0') && (charLetter <= '9') ) || (charLetter == '.')) 

此外,由于.不是数字,因此该表达式相当于:

if (!( (charLetter >= '0') && (charLetter <= '9') )) 

您可能认为&&不是||

if (!( (charLetter >= '0') && (charLetter <= '9' ) ) && (charLetter != '.'))

这意味着if(not_a_number AND not_a_full-stop)

答案 1 :(得分:0)

您可以double number = input.nextDouble();代替strNumber = input.next();。这样您就可以直接将number输入double而不是String

你必须在你的catch区块处理InputMismatchException,你很高兴。您无需验证是否包含.

答案 2 :(得分:0)

使用正则表达式会更容易:

bolNumeric = strNumber.matches("[1-9][0-9]*(\\.[0-9]{1,2})?");

说明:第一个数字必须在1-9之内。然后,根据您的需要(包括没有),可以跟随其他数字。这可选地后跟一个点,然后是至少一个,最多2个数字。