EugenyB

Scanner (самодельный и быстрый)

Feb 15th, 2012
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.90 KB | None | 0 0
  1. class Scanner {
  2.  
  3.    java.io.InputStream in;
  4.    char c;
  5.  
  6.    Scanner(java.io.InputStream in) {
  7.       this.in = in;
  8.       nextChar();
  9.    }
  10.  
  11.    void asserT(boolean e) {
  12.       if (!e) {
  13.          throw new Error();
  14.       }
  15.    }
  16.  
  17.    void nextChar() {
  18.       try {
  19.          c = (char)in.read();
  20.       } catch (IOException e) {
  21.          throw new Error(e);
  22.       }
  23.    }
  24.  
  25.    long nextLong() {
  26.       while (true) {
  27.          if ('0' <= c && c <= '9') {
  28.             break;
  29.          }
  30.          asserT(c != -1);
  31.          nextChar();
  32.       }
  33.       long value = c - '0';
  34.       nextChar();
  35.       while ('0' <= c && c <= '9') {
  36.          value *= 10;
  37.          value += c - '0';
  38.          nextChar();
  39.       }
  40.       return value;
  41.    }
  42.  
  43.    int nextInt() {
  44.       long longValue = nextLong();
  45.       int intValue = (int)longValue;
  46.       asserT(intValue == longValue);
  47.       return intValue;
  48.    }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment