Hey I have to ask the user for a purchase amount using JOptionPane and if they input more than two decimal places, nothing, characters, or more than one decimal point the program has to show an error message and stop.
How would I do this?
I don't want someone to write the program for me just a link that explains how I would do it
String PurchaseAmount = JOptionPane.showInputDialog(null, String.format("What is your purchase amount?"));
double PurchaseTotal = Double.parseDouble(PurchaseAmount);
String PaymentAmount = JOptionPane.showInputDialog(null, "What is the payment amount?");
double PaymentGiven = Double.parseDouble(PaymentAmount);
if the user inputs "12.526" or " " or "1.3.25" or "abc" I want the program to show an error message and stop.
Since this appears to be a confusing question or I'm asking it incorrectly these are my teachers directions exactly:
You'll want to write a method to validate the input. This can be done in steps:
First split the input on "."
(Alternatively, you can use a regular expression instead of a series of tests. This is probably a better solution, but I figure if you use one, you're going to have to maintain it, so you'll have to start by learning to write it.)
When you get back the validation, if the entry passes, parse it and get on with the next step. If false, exit in some sane way (preferably not by System.exit() - you'd like to be able to terminate in a graceful fashion)