blob: c22cbf3132ef904f4374a2893b139d8e29540ea6 (
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
|
//
// See ../README.md
//
//
// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file
//
import java.util.*;
import java.util.regex.Pattern;
public class ch1 {
public static void main (String [] args) {
Scanner scanner = new Scanner (System . in);
try {
while (true) {
String line = scanner . nextLine ();
if (!Pattern . matches ("^[-+]?[0-9]+$", line)) {
System . out . println ("not an integer");
continue;
}
if (Pattern . matches ("^[-+].*", line)) {
line = line . substring (1);
}
int ll = line . length ();
if (ll % 2 == 0) {
System . out . println ("even number of digits");
continue;
}
if (ll < 3) {
System . out . println ("too short");
continue;
}
System . out . println (line . substring ((ll - 3) / 2,
(ll + 3) / 2));
}
}
catch (Exception e) {
//
// EOF
//
}
}
}
|