Advertisement
mrScarlett

Verification and Validation

Jan 16th, 2017
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  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.  while (true){
  24.        System.out.println("Enter age.");
  25.        String age=userInput.next();
  26.        try{
  27.           int num=Integer.parseInt(age);
  28.            break;
  29.        }    
  30.        catch (NumberFormatException e)
  31.        {
  32.            System.out.println("You may only enter integers as an age. Try again.");
  33.            System.out.println(e.getMessage());
  34.            userInput.next();
  35.        }
  36.     }
  37.    
  38.     int []scores={3,22,99,56,77};
  39.     try{
  40.     for (int i=0;i<6; i++){
  41.         System.out.println(scores[i]);
  42.     }
  43. }
  44.     catch(ArrayIndexOutOfBoundsException e) {
  45.          System.out.println("Array is out of Bounds"+e);
  46.     }
  47.        
  48.         /*length check
  49.        Checks that the input contains a certain amount of characters
  50.         */
  51.        System.out.println("Enter name.");
  52.        String name = userInput.next();
  53.        while (true){
  54.        if (name.length()<3||name.length()>15){
  55.            System.out.println("You may only enter names between 3 and 15 characters,try again.");
  56.            name = userInput.next();
  57.        }    
  58.        else{
  59.        break;
  60.        }
  61. }
  62.        
  63.         /*range check
  64.         A RANGE CHECK checks that only numbers within a specified range are accepted. For
  65.         example percentage marks between 0 and 100 inclusive.
  66.         */
  67.        System.out.println("Enter percentage score.");
  68.        int score = userInput.nextInt();
  69.        while (true){
  70.        if (score<0||score>100){
  71.            System.out.println("You may only enter scores from 0 to 100, try again.");
  72.            score = userInput.nextInt();
  73.        }    
  74.        else{
  75.        break;
  76.        }
  77. }
  78.        
  79.         //presence check - checks that something has been input (not left blank)
  80.        System.out.println("Enter surname.");
  81.        String surname = userInput.next();
  82.        while (true){
  83.        if (surname.length()<0){ //this won't work in the java terminal window.
  84.            System.out.println("You cannot leave surname blank");
  85.            surname = userInput.next();
  86.        }    
  87.        else{
  88.        break;
  89.        }
  90. }
  91.        
  92.         //format check - checks that the input follows a specific format.
  93.        System.out.println("Enter phone number.");
  94.        String phone = userInput.next();
  95.        String pattern1="010";
  96.        String pattern2="011";
  97.        String pattern3="017";
  98.        int invalidChars=0;
  99.        if (phone.substring(0,3).equals(pattern1)||phone.substring(0,3).equals(pattern2)||phone.substring(0,3).equals(pattern3)){
  100.            if (phone.length()==11){
  101.                //character check
  102.                for (int x=0; x<phone.length(); x++){
  103.                   if (Character.isDigit(phone.charAt(x))){    
  104.                     }
  105.                   else{
  106.                       invalidChars+=1;
  107.                     }
  108.                 }
  109.                if (invalidChars>0){
  110.                    System.out.println("The numnber contains"+invalidChars+"invalid characters");
  111.             }
  112.             }else{
  113.                 System.out.println("invalid length");
  114.             }
  115.         }else{
  116.     System.out.println("invalid format, must begin 010,011 or 017");
  117. }
  118.  
  119.         //verification
  120.        System.out.println("Enter email address.");
  121.        String email1 = userInput.next();
  122.        System.out.println("Enter email again.");
  123.        String email2 = userInput.next();
  124.        if (email1.equals(email2)){
  125.            System.out.println("Email matched, they have been verified.");  
  126.     }else{
  127.         System.out.println("Emails do not match, they have NOT been verified.");  
  128.     }
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement