public static int stringToInt(String s) {
        // "-" starts as a valid integer, but has no digits.
        if (s.equals("-")) {
            throw new RuntimeException("illegal input");
        }

        boolean is_negative = s.charAt(0) == '-';
        int x = 0;
        for (int i = is_negative ? 1 : 0; i < s.length(); ++i) {
            if (Character.isDigit(s.charAt(i))) {
                x = x * 10 + s.charAt(i) - '0';
            } else {
                throw new RuntimeException("illegal input");
            }
        }
        return is_negative ? -x : x;
    }