diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2021-05-02 21:18:44 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2021-05-02 21:18:44 +0800 |
| commit | ced0756512f95aea89d82a1cf575350d0796d509 (patch) | |
| tree | f6fb0128ac0c2fcdfbff45030488005bec88a76c /challenge-110/cheok-yin-fung/java/PhoneNumber.java | |
| parent | 36318fa49b15eee8e9f9a66f03022009fdf12b99 (diff) | |
| download | perlweeklychallenge-club-ced0756512f95aea89d82a1cf575350d0796d509.tar.gz perlweeklychallenge-club-ced0756512f95aea89d82a1cf575350d0796d509.tar.bz2 perlweeklychallenge-club-ced0756512f95aea89d82a1cf575350d0796d509.zip | |
submission for wk 110
Diffstat (limited to 'challenge-110/cheok-yin-fung/java/PhoneNumber.java')
| -rw-r--r-- | challenge-110/cheok-yin-fung/java/PhoneNumber.java | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/challenge-110/cheok-yin-fung/java/PhoneNumber.java b/challenge-110/cheok-yin-fung/java/PhoneNumber.java new file mode 100644 index 0000000000..1dd7bccf60 --- /dev/null +++ b/challenge-110/cheok-yin-fung/java/PhoneNumber.java @@ -0,0 +1,70 @@ +// The Weekly Challenge 110 +// Task 1 Valid Phone Numbers + +import java.io.File; +import java.util.Scanner; +import java.io.FileNotFoundException; + +// Usage: java PhoneNumber [file name] + +public class PhoneNumber +{ + public static void main(String[] args) throws Exception + { + try { + File file = new File(args[0]); + Scanner sc = new Scanner(file); + while (sc.hasNextLine()) { + String num = sc.nextLine(); + if ( checkHead(num) && checkMid(num) && + num.substring(5).trim().length() == 10 && checkTail(num)) + System.out.println(num); + } + } catch (FileNotFoundException e) { + System.out.println(e.getMessage()); + System.exit(0); + } catch (IndexOutOfBoundsException e) { + System.out.println("Usage: java PhoneNumber [file name]"); + System.exit(0); + } + } + + private static boolean checkHead(String tel) + { + tel = tel.trim(); + if (tel.charAt(0) == '+' + && isNumeric(tel.charAt(1)) + && isNumeric(tel.charAt(2)) ) + return true; + + if (tel.charAt(0) == '(' && tel.charAt(3) == ')' + && isNumeric(tel.charAt(1)) + && isNumeric(tel.charAt(2))) + return true; + + boolean temp_bl = true; + for (int a = 0; a < 4 && temp_bl ; a++) + temp_bl = temp_bl && isNumeric(tel.charAt(a)); + return temp_bl; + } + + private static boolean checkMid(String tel) + { + return tel.charAt(4) == ' '; + } + + private static boolean checkTail(String tel) + { + boolean temp_bl = true; + for (int i = 0; i < 10 && temp_bl ; i++) + temp_bl = temp_bl && isNumeric(tel.charAt(5+i)); + return temp_bl; + } + + private static boolean isNumeric(char ch) + { + if (ch >= '0' && ch <= '9') return true; + else return false; + } + +} |
