aboutsummaryrefslogtreecommitdiff
path: root/challenge-110/cheok-yin-fung/java/PhoneNumber.java
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2021-05-03 18:31:36 +0800
committer冯昶 <fengchang@novel-supertv.com>2021-05-03 18:31:36 +0800
commit81252bda7fb7bcc9e9e153a6b3d268ab8c1a38c8 (patch)
treee4df369a6349802da33aa6d94b7fe745041a9955 /challenge-110/cheok-yin-fung/java/PhoneNumber.java
parent0142974e5f11adadbaa7ca8d71de9db345318519 (diff)
parent0381a39b17ccd040302474f25d3c1cbbef703327 (diff)
downloadperlweeklychallenge-club-81252bda7fb7bcc9e9e153a6b3d268ab8c1a38c8.tar.gz
perlweeklychallenge-club-81252bda7fb7bcc9e9e153a6b3d268ab8c1a38c8.tar.bz2
perlweeklychallenge-club-81252bda7fb7bcc9e9e153a6b3d268ab8c1a38c8.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-110/cheok-yin-fung/java/PhoneNumber.java')
-rw-r--r--challenge-110/cheok-yin-fung/java/PhoneNumber.java70
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;
+ }
+
+}