package io; import java.lang.*; import java.io.* ; /** for flexible reading of InputStream; all methods are static. Works fine but does not handle re-positioning of the pointer after reading past the end-of-word the terminating non-letterOrDigit character; All methods come in three versions: 1) meth(InputStream ip) for reading from ip; 2) meth() for reading from terminal; 3) askMeth(String s) for reading from terminal with a prompt s; */ public class les { private static char EOF= ((char)(-1)); /** returns next character */ public static final char tegn(InputStream in) { char c='0'; int ch=0; try{ ch= in.read(); c= (char)ch; } catch(IOException e){} return c; } /** returns next character distinct from space ' '*/ public static final char tegnNB(InputStream in) { char c='0'; int ch=0; try{ ch= in.read(); c= (char)ch; while (c==' ') {ch= in.read(); c=(char)ch;} } catch(IOException e){} return c; } /** returns next non-formatting character (distinct from space, \n, \r, \t) */ public static char tegnNF(InputStream in) { char c=' '; try{ c= (char)in.read(); while (c!=EOF && (c==' ' || c=='\n' || c=='\r' || c=='\t')) c= (char)in.read(); } catch(IOException e){} return c; } /** returns next integer - skips possible initial non-digit characters; reads one character past the last digit !!! */ public static final int htall(InputStream in) { int r=0, ch, s=0; try{ ch= in.read()-48; while (ch<0 || ch>9) {s=ch; ch= in.read()-48;} r= ch; ch= in.read()-48; while (ch >= 0 && ch <= 9) {r= (r*10)+ch; ch= in.read()-48; } } catch(IOException e){} s= s+48; if ((char)s == '-') {r= -r;} return r; } /** returns boolean - false iff next NB character is 'n', 'N', '0' or '-' */ public static final boolean bool(InputStream in) { char c= tegnNB(in); if ( c=='n' || c=='N' || c=='0' || c=='-') return false; else return true; } /** reads next word - non-SPACE characters between SPACEs; skips initial SPACEs */ public static final String ord(InputStream in) { char c= tegn(in); String s= new String(""); while (c==' ') { c= tegn(in); } while (c != ' ') {s= s+c; c= tegn(in);} return s; } /** read word starting with a letter and containing only letters or digits; skips initial non-letters; reads one extra character past the last letter/digit !!! */ public static final String ordLD(InputStream in) { int ch, i; char c; String s= new String(""); try{ ch= in.read(); c= (char)ch; i=1; while (!Character.isLetter(c)) { ch= in.read(); c=(char)ch; i= i+1;} while (Character.isLetterOrDigit(c)) {s=s+c; ch= in.read();c=(char)ch;i=i+1;} } catch(IOException e){System.out.println("smth. wrong !!!");} return s; } /* above methods but now for reading from the terminal */ /** as 'htall(InputStream in)' but reads from terminal */ public static final int htall() { return htall(System.in); } /** as 'tegn(InputStream in)' but reads from terminal */ public static final char tegn() { return tegn(System.in); } /** as 'tegnNB(InputStream in)' but reads from terminal */ public static final char tegnNB() { return tegnNB(System.in); } /** as 'tegnNF(InputStream in)' but reads from terminal */ public static final char tegnNF() { return tegnNF(System.in); } /** as 'bool(InputStream in)' but reads from terminal */ public static final boolean bool() { return bool(System.in); } /** as 'ord(InputStream in)' but reads from terminal */ public static final String ord() { return ord(System.in); } /** as 'ordLD(InputStream in)' but reads from terminal - works fine*/ public static final String ordLD() { return ordLD(System.in); } /* above methods for reading from the terminal with an 'ask for' prompt */ /** as 'htall()' but with a prompt */ public static final int askHtall(String s) { System.out.print(s+": "); return htall(System.in); } /** as 'bool()' but with a prompt */ public static final boolean askBool(String s) { System.out.print(s+": "); return bool(System.in); } /** as 'ord()' but with a prompt */ public static final String askOrd(String s) { System.out.print(s+": "); return ord(System.in); } /** as 'ordLD()' but with a prompt */ public static final String askOrdLD(String s) { System.out.print(s+": "); return ordLD(System.in); } /** as 'tegn()' but with a prompt */ public static final char askTegn(String s) { System.out.print(s+": "); return tegn(System.in); } /** as 'tegnNB()' but with a prompt */ public static final char askTegnNB(String s) { System.out.print(s+": "); return tegnNB(System.in); } /** as 'tegnNF()' but with a prompt */ public static final char askTegnNF(String s) { System.out.print(s+": "); return tegnNF(System.in); } }