Log in

View Full Version : How can I detect whether an inputted number is in the correct format?


GratefulDeadpoo
May 23, 2016, 03:47 PM
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:



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.

CravenMorhead
May 24, 2016, 07:07 AM
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 (https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html)suggests that you shouldn't use the constructor. Maybe look at that page and see if it helps.