Source of Validate.java


  1: // Fig. 29.21: Validate.java
  2: // Validate user information using regular expressions.
  3: import java.util.Scanner;

  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();

 26:       // validate user input and display error message
 27:       System.out.println( "\nValidate Result:" );

 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

 48: /*
 49:  **************************************************************************
 50:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 51:  * Pearson Education, Inc. All Rights Reserved.                           *
 52:  *                                                                        *
 53:  * DISCLAIMER: The authors and publisher of this book have used their     *
 54:  * best efforts in preparing the book. These efforts include the          *
 55:  * development, research, and testing of the theories and programs        *
 56:  * to determine their effectiveness. The authors and publisher make       *
 57:  * no warranty of any kind, expressed or implied, with regard to these    *
 58:  * programs or to the documentation contained in these books. The authors *
 59:  * and publisher shall not be liable in any event for incidental or       *
 60:  * consequential damages in connection with, or arising out of, the       *
 61:  * furnishing, performance, or use of these programs.                     *
 62:  **************************************************************************
 63: */