Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   How can I detect whether an inputted number is in the correct format? (https://www.askmehelpdesk.com/showthread.php?t=824973)

  • May 23, 2016, 03:47 PM
    GratefulDeadpoo
    How can I detect whether an inputted number is in the correct format?
    I'm creating a program where it detects whether an inputted number matches one of the pre-defined formats. Every time I execute my code and input a number, it will say "Invalid input." I'm not sure if it's due to boolean logic or me not using DecimalFormat correctly. Here's the code I have thus far:


    Code:

        do
            {
                    System.out.println("Enter the amount you wish to deposit  ==>>  $");
                    amount = inputDW.nextLine();
                    moneyInput = new DecimalFormat (amount);
                    corInput1 = new DecimalFormat ("000.00");
                    corInput2 = new DecimalFormat ("00.00");
                    corInput3 = new DecimalFormat ("0.00");
                    if((moneyInput.equals(corInput1) == false) && (moneyInput.equals(corInput2) == false) && (moneyInput.equals(corInput3) == false))
                    {
                        System.out.println("Invalid input");
                    }
            }
            while((moneyInput.equals(corInput1) == false) && (moneyInput.equals(corInput2) == false) && (moneyInput.equals(corInput3) == false));

    Note: It's obviously only part of my entire code for the program.
  • May 24, 2016, 07:07 AM
    CravenMorhead
    I believe you're using the decimalformat incorrectly. I haven't used this class, let alone java, in more years then I care to think about. The article here suggests that you shouldn't use the constructor. Maybe look at that page and see if it helps.

  • All times are GMT -7. The time now is 12:10 AM.