aboutsummaryrefslogtreecommitdiff
path: root/challenge-117/abigail/java/ch-1.java
blob: 6a03391c2ea7d5526af7aee28d7f12cff786e5d8 (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
//
// See ../README.md
//

//
// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file
//

import java.util.*;

public class ch1 {
    public static void main (String [] args) {
        Scanner scanner = new Scanner (System . in);
        int sum    = 0;
        int SUM_15 = 120;
        try {
            while (true) {
                String line = scanner . nextLine ();
                //
                // Googling suggest Java has a gazillion methods to
                // convert string to integers -- and they all crap
                // out if the string starts with a number, and has
                // anything trailing. None of them actually similate atoi.
                // 
                // So, handrolling it is!
                //
                int i, n = 0;
                for (i = 0; i < line . length (); i ++) {
                    if (line . charAt (i) < "0" . charAt (0) ||
                        line . charAt (i) > "9" . charAt (0)) {
                        break;
                    }
                    n *= 10;
                    n += line . charAt (i) - "0" . charAt (0);
                }
                sum += n;
            }
        }
        catch (Exception e) {
            System . out . println (SUM_15 - sum);
        }
    }
}