Java Reference
In-Depth Information
35
else if (!ValidateInput.validateCity(city))
36
System.out.println( "Invalid city" );
37
else if (!ValidateInput.validateState(state))
38
System.out.println( "Invalid state" );
39
else if (!ValidateInput.validateZip(zip))
40
System.out.println( "Invalid zip code" );
41
else if (!ValidateInput.validatePhone(phone))
42
System.out.println( "Invalid phone number" );
43
else
44
System.out.println( "Valid input. Thank you." );
45
}
46
} // end class Validate
Please enter first name:
Jane
Please enter last name:
Doe
Please enter address:
123 Some Street
Please enter city:
Some City
Please enter state:
SS
Please enter zip:
123
Please enter phone:
123-456-7890
Validate Result:
Invalid zip code
Please enter first name:
Jane
Please enter last name:
Doe
Please enter address:
123 Some Street
Please enter city:
Some City
Please enter state:
SS
Please enter zip:
12345
Please enter phone:
123-456-7890
Validate Result:
Valid input. Thank you.
Fig. 14.21 | Input and validate data from user using the ValidateInput class. (Part 2 of 2.)
Figure 14.20 validates user input. Line 9 validates the first name. To match a set of
characters that does not have a predefined character class, use square brackets, [] . For
example, the pattern "[aeiou]" matches a single character that's a vowel. Character ranges
Search WWH ::




Custom Search