This is just another option for this task which also contains User entry validation. If the User enters the required data incorrectly then that User is informed of such and given the opportunity to attempt the entry again or enter q
to quit.
The provided runnable demo below allows for single country state or province two letter abbreviations or overall North American (Canada, USA, and Mexico) abbreviations which the demo actually utilizes. The means to easily modify the demo code for a single specific country is only a few keystrokes away.
The tax rate is of double
data type and the code validates User entry for unsigned integer or floating point values from 0 to 100 (percent) only. Floating point entry is allowed since US states contain a floating point sales tax rate.
When the entry prompt is displayed, the User is expected to enter a two letter abbreviation (in any letter case) of a state or province and on the same entry line, the tax rate for that same state or province separated with a whitespace or tab. The following would be valid entries:
QC 14.975 [Quebec, Canada]
MO 8.25 [Missouri, USA or Morelos, Mexico]
bc 12 [British Columbia, Canada or Baja California, Mexico]
sk 11 [Saskatchewan, Canada]
ON 13 [Ontario, Canada]
GR 0 [Guerrero, Mexico]
q [Used to quit the application]
Invalid entries might be:
QC14.975 [No whitespace]
MO, 8.25 [comma used]
ZA 6f [A non-digit in tax rate]
bv 12 [No such state or province]
sk 101 [Tax rate out of range (0 to 100 allowed only)]
ON -13 [signed tax rate value]
7.5 [no state or province supplied]
YU [no tax rate supplied (minimum 0 to be supplied)]
[nothing supplied (just Enter key was hit)]
Here is the runnable code:
public class StateProvinceAbbrevAndTaxRateDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
java.util.Scanner in = new java.util.Scanner(System.in);
/* North American Postal Abbreviations used for entry validation...
Select your flavor or use all three as used in this demo: */
String canada = "\\bnl|pe|ns|nb|qc|on|mb|sk|ab|bc|yt|nt|nu\\b";
String usa = "\\bAL|AK|AZ|AR|CA|CO|CT|DE|FL|GA|HI|ID|IL|IN|IA|KS|KY|LA|"
+ "ME|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|"
+ "PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY\\b";
String canadaUSA = "\\bAL|AK|AZ|AR|AB|BC|CA|CO|CT|DE|FL|GA|HI|ID|IL|IN|"
+ "IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|MB|NE|NV|NH|NJ|"
+ "NM|NY|NC|ND|NL|NS|NB|NT|NU|OH|OK|OR|ON|PA|PE|QC|RI|"
+ "SC|SD|SK|TN|TX|UT|VT|VA|WA|WV|WI|WY|YT\\b";
String mexico = "\\bAG|BC|BS|CM|CS|CH|CO|CL|DF|DG|GT|GR|JA|EM|MI|MO|"
+ "NA|NL|OA|PU|QT|QR|SL|SI|SO|TB|TM|TL|VE|YU|ZA\\b";
// =======================================================================
/* Used for entry Validation:
Desired abbreviations added to Regular Expression (regex).
The regex below covers abbreviations for all of North America
(Canada/USA/Mexico). Modify the expression to suit your needs. */
String regEx = "(?i)q|(" + canada + "|" + usa + "|" + mexico + ")" // valid abbbreviation
+ "\\s+" // must have 'at least' one space (could have more)...
+ "(\\b([0-9]|[1-9][0-9]|100)(\\.\\d*)?\\b)"; // tax rate must be inclusively between 0 and 100 percent.
String input = "";
while (input.isEmpty()) {
System.out.println("Enter the state or province postal abbreviation and tax");
System.out.println("rate separated with a space (ex: BC 7 or: ny 8.52).");
System.out.print( "Enter 'Q' to quit: --> ");
input = in.nextLine();
if (!input.matches(regEx)) {
System.out.println("Invalid Entry (" + input + ")! Please try again...");
System.out.println();
input = "";
}
}
System.out.println();
// Is 'q' (to quit) supplied?
if (input.equalsIgnoreCase("q")) {
System.out.println("Quitting... Bye-Bye");
System.exit(0);
}
/* Split the User's data entry into a String Array */
String[] inputParts = input.split("\\s+");
/* Apply the String Array elements to their respective
variables (converting where required). */
String stateProv = inputParts[0].toUpperCase();
double tax = Double.parseDouble(inputParts[1]);
// Display results within the Console Window.
System.out.println("State/Province: --> " + stateProv);
System.out.println("Tax Rate: --> " + tax + "%");
}
}
In the above code, the String#matches() method (along with a rather intense Regular Expression) is used on the User entered string to carry out entry validation. The string variable regEx
holds the Regular Expression used:
String regEx = "(?i)q|(" + canada + "|" + usa + "|" + mexico + ")" // valid abbbreviation
+ "\\s+" // must have 'at least' one space (could have more)...
+ "(\\b([0-9]|[1-9][0-9]|100)(\\.\\d*)?\\b)"; // tax rate must be inclusively between 0 and 100 percent.