lewile1

Week 4 Assignment

Jan 12th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. package u04a1_validateuserinput;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  *
  7.  * @author leonlewis
  8.  */
  9. public class U04A1_ValidateUserInput {
  10.  
  11.     public static void main(String[] args) {
  12.         Scanner input = new Scanner(System.in);
  13.         String userInput;
  14.         boolean isValid = true;
  15.        
  16.         System.out.print("Enter a course code to validate (e.g. IT4782): ");
  17.         userInput = input.nextLine();
  18.        
  19.         /* only to be used with different algorithm
  20.         if(ValidateInput(userInput)){
  21.             System.out.println("Course code " + userInput + " is valid.");
  22.         }
  23.         */
  24.        
  25.         if (userInput.length() != 6){
  26.             System.out.println("The course code is not the right length.");
  27.         }
  28.         else{
  29.             if(Character.toLowerCase(userInput.charAt(0)) != 'i'){
  30.                 System.out.println("First character is not an I or an i");
  31.                 isValid = false;
  32.             }
  33.             if(Character.toLowerCase(userInput.charAt(1)) != 't'){
  34.                 System.out.println("Second character is not a T or a t");
  35.                 isValid = false;
  36.             }
  37.             if(!Character.isDigit(userInput.charAt(2))){
  38.                 System.out.println("Third character is not a digit");
  39.                 isValid = false;
  40.             }
  41.             if(!Character.isDigit(userInput.charAt(3))){
  42.                 System.out.println("Fourth character is not a digit");
  43.                 isValid = false;
  44.             }
  45.             if(!Character.isDigit(userInput.charAt(4))){
  46.                 System.out.println("Fifth character is not a digit");
  47.                 isValid = false;
  48.             }
  49.             if(!Character.isDigit(userInput.charAt(5))){
  50.                 System.out.println("Sixth character is not a digit");
  51.                 isValid = false;
  52.             }
  53.             if(isValid){
  54.                 System.out.println("Course code " + userInput + " is valid.");
  55.             }
  56.            
  57.         }
  58.     }
  59.    
  60.     // Different algorithm because I did not like reusing
  61.     // the if statement over and over again.
  62.     private static boolean ValidateInput(String userInput){
  63.         String namedArray[] = {"First", "Second", "Third", "Fourth", "Fifth", "Sixth" };
  64.         String reason[] = {"not an i or an I", "not a t or a T", "not a digit"};
  65.         boolean isValid = true;
  66.        
  67.         for(int i = 0; i < userInput.length(); i++){
  68.             if(i == 0 || i == 1){
  69.                 char tempChar = Character.toLowerCase(userInput.charAt(i));
  70.                 String it = "it";
  71.                 if(tempChar != it.charAt(i)){
  72.                     System.out.println(namedArray[i] + " character is " + reason[i]);
  73.                     isValid = false;
  74.                 }
  75.             }else {
  76.                 if(!Character.isDigit(userInput.charAt(i))){
  77.                     System.out.println(namedArray[i] + " character is " + reason[2]);
  78.                     isValid = false;
  79.                 }
  80.             }
  81.         }
  82.         return isValid;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment