Validate input data
Validate data
Validate a single entry
How to validate an entry
- When a user enters data, that data usually needs to be checked to make sure that it is valid. This is known as data validation.
- When an entry is invalid, the program needs to display an error message and give the user another chance to enter valid data. This needs to be repeated until the entry is valid.
- One way to code this type of validation routine is to use a while loop.
- Two common types of validity checking for a numeric entry are (1) to make sure that the entry has a valid numeric format, and (2) to make sure that the entry is within a valid range (known as range checking).
Code that gets a valid double value within a specified range
Scanner sc = new Scanner(System.in); double subtotal = 0.0; boolean isValid = false; while (isValid == false) { // get a valid double value System.out.print("Enter subtotal: "); if (sc.hasNextDouble()) { subtotal = sc.nextDouble(); isValid = true; } else { System.out.println( "Error! Invalid number. Try again."); } sc.nextLine(); // discard any other data entered on the line // check the range of the double value if (isValid == true && subtotal <= 0) { System.out.println( "Error! Number must be greater than 0."); isValid = false; } else if (isValid == true && subtotal >= 10000) { System.out.println( "Error! Number must be less than 10000."); isValid = false; } }
import java.util.Scanner;
import java.util.InputMismatchException;
public class InvoiceApp
{
public static void main(String[] args)
{
// welcome the user to the program
System.out.println("Welcome to the Invoice Total Calculator");
System.out.println(); // print a blank line
// create a Scanner object named sc
Scanner sc = new Scanner(System.in);
// perform invoice calculations until choice isn't equal to "y" or "Y"
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
double subtotal = 0.0;
boolean isValid = false;
while (isValid == false)
{
// get a valid double value
System.out.print("Enter subtotal: ");
if (sc.hasNextDouble())
{
subtotal = sc.nextDouble();
isValid = true;
}
else
{
System.out.println("Error! Invalid number. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
// check the range of the double value
if (isValid == true && subtotal <= 0)
{
System.out.println(
"Error! Number must be greater than 0.");
isValid = false;
}
else if (isValid == true && subtotal >= 10000)
{
System.out.println("Error! Number must be less than 10000.");
isValid = false;
}
}
// calculate the discount amount and total
double discountPercent= 0.0;
if (subtotal >= 200)
discountPercent = .2;
else if (subtotal >= 100)
discountPercent = .1;
else
discountPercent = 0.0;
double discountAmount = subtotal * discountPercent;
double total = subtotal - discountAmount;
// display the discount amount and total
String message = "Discount percent: " + discountPercent + "\n"
+ "Discount amount: " + discountAmount + "\n"
+ "Invoice total: " + total + "\n";
System.out.println(message);
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
}
}