01: package org.jgroups.tests;
02:
03: import java.io.*;
04:
05: /**
06: * Checks whether numbers in a stream are monotonically increasing, e.g.
07: * <pre>
08: * 1
09: * 2
10: * 3
11: * 4
12: * </pre>
13: * @author Bela Ban
14: * @version $Id: CheckMonotonicallyIncreasingNumbers.java,v 1.2 2006/01/16 13:01:26 belaban Exp $
15: */
16: public class CheckMonotonicallyIncreasingNumbers {
17:
18: static int check(InputStream in) throws IOException {
19: Reader r = new BufferedReader(new InputStreamReader(in));
20: StreamTokenizer st = new StreamTokenizer(r);
21: int i, cnt = 0, num = 0, tmp, incorrect = 0;
22: boolean first_read = false;
23:
24: while (true) {
25: i = st.nextToken();
26: if (i == StreamTokenizer.TT_EOF)
27: break;
28: tmp = (int) st.nval;
29: if (!first_read) {
30: first_read = true;
31: } else {
32: if (tmp != num + 1) {
33: System.err.println("Number read: " + tmp
34: + ", previous number: " + num
35: + " (lineno: " + st.lineno() + ")");
36: incorrect++;
37: }
38: }
39: num = tmp;
40: cnt++;
41: if (cnt > 0 && cnt % 1000 == 0)
42: System.out.println("read " + cnt + " numbers");
43: }
44: return incorrect;
45: }
46:
47: public static void main(String[] args) throws IOException {
48: String file = null;
49: for (int i = 0; i < args.length; i++) {
50: if (args[i].equals("-file")) {
51: file = args[++i];
52: continue;
53: }
54: help();
55: return;
56: }
57: FileInputStream fis = new FileInputStream(file);
58: int incorrect = CheckMonotonicallyIncreasingNumbers.check(fis);
59: if (incorrect == 0) {
60: System.out
61: .println("OK, all numbers are monotonically increasing");
62: } else {
63: System.err.println("Failure: there are " + incorrect
64: + " incorrect numbers");
65: }
66: fis.close();
67: }
68:
69: private static void help() {
70: System.out
71: .println("CheckMonotonicallyIncreasingNumbers [-help] [-file <filename>]");
72: }
73: }
|