Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class ValidationVerification
- {
- public static void main(String[] args) {
- Scanner userInput = new Scanner(System.in);
- /*type check
- A TYPE CHECK checks that the data entered is of a given data type,
- for example number of brothers or sisters would be an integer (whole number).
- */
- while (true){
- System.out.println("Enter age.");
- try{
- int age = userInput.nextInt();
- break;
- }
- catch (InputMismatchException e)
- {
- System.out.println("You may only enter integers as an age. Try again.");
- userInput.next();
- }
- }
- /*length check
- Checks that the input contains a certain amount of characters
- */
- System.out.println("Enter name.");
- String name = userInput.next();
- while (true){
- if (name.length()<3||name.length()>15){
- System.out.println("You may only enter names between 3 and 15 characters,try again.");
- name = userInput.next();
- }
- else{
- break;
- }
- }
- /*range check
- A RANGE CHECK checks that only numbers within a specified range are accepted. For
- example percentage marks between 0 and 100 inclusive.
- */
- System.out.println("Enter percentage score.");
- int score = userInput.nextInt();
- while (true){
- if (score<0||score>100){
- System.out.println("You may only enter scores from 0 to 100, try again.");
- score = userInput.nextInt();
- }
- else{
- break;
- }
- }
- //presence check - checks that something has been input (not left blank)
- System.out.println("Enter surname.");
- String surname = userInput.next();
- while (true){
- if (surname.length()<0){ //this won't work in the java terminal window.
- System.out.println("You cannot leave surname blank");
- surname = userInput.next();
- }
- else{
- break;
- }
- }
- //format check - checks that the input follows a specific format.
- System.out.println("Enter phone number.");
- String phone = userInput.next();
- String pattern1="010";
- String pattern2="011";
- String pattern3="017";
- int invalidChars=0;
- if (phone.substring(0,3).equals(pattern1)||phone.substring(0,3).equals(pattern2)||phone.substring(0,3).equals(pattern3)){
- if (phone.length()==11){
- //character check
- for (int x=0; x<phone.length(); x++){
- if (Character.isDigit(phone.charAt(x))){
- }
- else{
- invalidChars+=1;
- }
- }
- if (invalidChars>0){
- System.out.println("The numnber contains"+invalidChars+"invalid characters");
- }
- }else{
- System.out.println("invalid length");
- }
- }else{
- System.out.println("invalid format, must begin 010,011 or 017");
- }
- //verification
- System.out.println("Enter email address.");
- String email1 = userInput.next();
- System.out.println("Enter email again.");
- String email2 = userInput.next();
- if (email1.equals(email2)){
- System.out.println("Email matched, they have been verified.");
- }else{
- System.out.println("Emails do not match, they have NOT been verified.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement