Advertisement
mrScarlett

verification/validation 2017

Nov 14th, 2017
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2. public class ValidationVerification
  3. {
  4.     public static void main(String[] args) {
  5.         Scanner userInput = new Scanner(System.in);
  6.        
  7.         /*type check
  8.         A TYPE CHECK checks that the data entered is of a given data type,
  9.         for example number of brothers or sisters would be an integer (whole number).
  10.         */
  11.        while (true){
  12.        System.out.println("Enter age.");
  13.        try{
  14.            int age = userInput.nextInt();
  15.            break;
  16.        }    
  17.        catch (InputMismatchException e)
  18.        {
  19.            System.out.println("You may only enter integers as an age. Try again.");
  20.            userInput.next();
  21.        }
  22. }
  23.  
  24.        
  25.        /*length check
  26.        Checks that the input contains a certain amount of characters
  27.         */
  28.        System.out.println("Enter name.");
  29.        String name = userInput.next();
  30.        while (true){
  31.        if (name.length()<3||name.length()>15){
  32.            System.out.println("You may only enter names between 3 and 15 characters,try again.");
  33.            name = userInput.next();
  34.        }    
  35.        else{
  36.        break;
  37.        }
  38. }
  39.        
  40.         /*range check
  41.         A RANGE CHECK checks that only numbers within a specified range are accepted. For
  42.         example percentage marks between 0 and 100 inclusive.
  43.         */
  44.        System.out.println("Enter percentage score.");
  45.        int score = userInput.nextInt();
  46.        while (true){
  47.        if (score<0||score>100){
  48.            System.out.println("You may only enter scores from 0 to 100, try again.");
  49.            score = userInput.nextInt();
  50.        }    
  51.        else{
  52.        break;
  53.        }
  54. }
  55.        
  56.         //presence check - checks that something has been input (not left blank)
  57.        System.out.println("Enter surname.");
  58.        String surname = userInput.next();
  59.        while (true){
  60.        if (surname.length()<0){ //this won't work in the java terminal window.
  61.            System.out.println("You cannot leave surname blank");
  62.            surname = userInput.next();
  63.        }    
  64.        else{
  65.        break;
  66.        }
  67. }
  68.        
  69.         //format check - checks that the input follows a specific format.
  70.        System.out.println("Enter phone number.");
  71.        String phone = userInput.next();
  72.        String pattern1="010";
  73.        String pattern2="011";
  74.        String pattern3="017";
  75.        int invalidChars=0;
  76.        if (phone.substring(0,3).equals(pattern1)||phone.substring(0,3).equals(pattern2)||phone.substring(0,3).equals(pattern3)){
  77.            if (phone.length()==11){
  78.                //character check
  79.                for (int x=0; x<phone.length(); x++){
  80.                   if (Character.isDigit(phone.charAt(x))){    
  81.                     }
  82.                   else{
  83.                       invalidChars+=1;
  84.                     }
  85.                 }
  86.                if (invalidChars>0){
  87.                    System.out.println("The numnber contains"+invalidChars+"invalid characters");
  88.             }
  89.             }else{
  90.                 System.out.println("invalid length");
  91.             }
  92.         }else{
  93.     System.out.println("invalid format, must begin 010,011 or 017");
  94. }
  95.  
  96.         //verification
  97.        System.out.println("Enter email address.");
  98.        String email1 = userInput.next();
  99.        System.out.println("Enter email again.");
  100.        String email2 = userInput.next();
  101.        if (email1.equals(email2)){
  102.            System.out.println("Email matched, they have been verified.");  
  103.     }else{
  104.         System.out.println("Emails do not match, they have NOT been verified.");  
  105.     }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement