Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

ISBN:
0-13-148398-6
© 2005
pages: 1576
Buy the Book!
Amazon logo
InformIT logo

This tutorial presents Java powerful regular-expression processing capabilities using class Pattern, class Matcher and class String's matches method. This tutorial is intended for students and developers who are familiar with basic Java string-processing techniques.

Download the code for this tutorial here.

[Note: This tutorial is an excerpt (Section 29.7) of Chapter 29, Strings, Characters and Regular Expressions, from our textbook Java How to Program, 6/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp.1378-1387. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

29.7 Regular Expressions, Class Pattern and Class Matcher (Continued)

   1  // Fig. 29.21: Validate.java
2 // Validate user information using regular expressions.
3 import java.util.Scanner;
4
5 public class Validate
6 {
7 public static void main( String[] args )
8 {
9 // get user input
10 Scanner scanner = new Scanner( System.in );
11 System.out.println( "Please enter first name:" );
12 String firstName = scanner.nextLine();
13 System.out.println( "Please enter last name:" );
14 String lastName = scanner.nextLine();
15 System.out.println( "Please enter address:" );
16 String address = scanner.nextLine();
17 System.out.println( "Please enter city:" );
18 String city = scanner.nextLine();
19 System.out.println( "Please enter state:" );
20 String state = scanner.nextLine();
21 System.out.println( "Please enter zip:" );
22 String zip = scanner.nextLine();
23 System.out.println( "Please enter phone:" );
24 String phone = scanner.nextLine();
25
26 // validate user input and display error message
27 System.out.println( "\nValidate Result:" );
28
29 if ( !ValidateInput.validateFirstName( firstName ) )
30 System.out.println( "Invalid first name" );
31 else if ( !ValidateInput.validateLastName( lastName ) )
32 System.out.println( "Invalid last name" );
33 else if ( !ValidateInput.validateAddress( address ) )
34 System.out.println( "Invalid address" );
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 } // end main
46 } // end class Validate
 Fig. 29.21  Inputs and validates data from user using the ValidateInput class.

 
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.


Page 1 | 2 | 3 | 4 | 5
Return to Tutorial Index