aboutsummaryrefslogtreecommitdiff
path: root/challenge-135/mohammad-anwar/java/MiddleThreeDigits.java
blob: 74eec8fc78465b2da606f553933db3f73b393278 (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
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*

Week 135:

    https://theweeklychallenge.org/blog/perl-weekly-challenge-135

Task #1: Middle 3-digits

    You are given an integer.

    Write a script find out the middle 3-digits of the given integer, if possible otherwise throw sensible error.

*/

class MiddleThreeDigits {

    public static void main(String[] args) {
        String p = System.getProperty("number", "1234567");

        Pattern pattern = Pattern.compile("\\-?\\d+");
        if (pattern.matcher(p).find()) {

            int n = Math.abs(Integer.parseInt(p));
            int l = countDigits(n);
            if (l == 1) {
                System.out.println("ERROR: Too short.");
            }
            else if (l % 2 == 0) {
                System.out.println("ERROR: Even number of digists.");
            }
            else {
                int i = (l / 2) - 1;
                System.out.println(String.valueOf(n).substring(i, i+3));
            }

        }
        else {
            System.out.println("ERROR: Invalid number.");
        }
    }

    public static int countDigits(int n) {
        int l = 0;
        for(; n != 0; n /= 10, ++l);
        return l;
    }

}