Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. public static boolean isPostalCode(String code) {
  2.        
  3.         int length = code.length();
  4.         String newcode = code;
  5.        
  6.         if(length == 7) {
  7.            
  8.             newcode = code.replaceAll(" ", ""); // if it has 7 characters replace the middle space with nothing so it makes it 6 chars long
  9.            
  10.         }
  11.                
  12.         char temp;
  13.         int nums = 0, letters = 0;
  14.        
  15.         for(int i = 1; i<length; i=i+2) { // loop for checking digit starting from 1 to 6 going 1 3 5
  16.            
  17.             temp = newcode.charAt(i);
  18.             if(Character.isDigit(temp))
  19.                 nums++; // to check how many of the chars were digits
  20.        
  21.         }
  22.         for(int i = 0; i<length-1; i=i+2) { // loop for checking letters from 0 to 4 going 0 2 4
  23.            
  24.             temp = newcode.charAt(i);
  25.             if(Character.isLetter(temp))
  26.                 letters++; // add to num of letters
  27.        
  28.         }
  29.        
  30.         if(nums == letters && nums == 3) // if three letters and found and 3 numbers are also found return true
  31.                 return true;
  32.        
  33.  
  34.        
  35.         return false;
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement