Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static boolean isPostalCode(String code) {
- int length = code.length();
- String newcode = code;
- if(length == 7) {
- newcode = code.replaceAll(" ", ""); // if it has 7 characters replace the middle space with nothing so it makes it 6 chars long
- }
- char temp;
- int nums = 0, letters = 0;
- for(int i = 1; i<length; i=i+2) { // loop for checking digit starting from 1 to 6 going 1 3 5
- temp = newcode.charAt(i);
- if(Character.isDigit(temp))
- nums++; // to check how many of the chars were digits
- }
- for(int i = 0; i<length-1; i=i+2) { // loop for checking letters from 0 to 4 going 0 2 4
- temp = newcode.charAt(i);
- if(Character.isLetter(temp))
- letters++; // add to num of letters
- }
- if(nums == letters && nums == 3) // if three letters and found and 3 numbers are also found return true
- return true;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement