aboutsummaryrefslogtreecommitdiff
path: root/challenge-110/cheok-yin-fung/java/PhoneNumber.java
blob: 1dd7bccf602efae81118beeb43c4112b7d2e0114 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;
    }

}