/** This classes buffers the input and provides support for reading type double values that are terminated by either a comma or close paren. Author: Charlie McDowell */ import tio.*; class InputBuffer { private char[] buffer; int index; /** Construct the initial buffer and fill it. */ InputBuffer() { fillBuffer(); } /** Read the next nonwhite character from the buffer. @return The next nonwhite input character, or -1 if no more input. */ int readCharNonwhite() { int c = readChar(); while (c != -1 && Character.isWhitespace((char)c)) c = readChar(); return c; } /** Read the next input character. DO NOT skip white space. @return The next input character or -1 if no more input. */ int readChar() { if (index >= buffer.length) { fillBuffer(); if ( buffer != null) { return buffer[index++]; } else return -1; } else return buffer[index++]; } /** Read a double, where the double is assumed to be followed by either a comma or a right parenthesis. The buffer index is left pointing at the comma or the right parenthesis. @return The double value read. */ double readDouble() { int end = commaOrCloseParen(); String s = new String(buffer, index, end - index); s = s.trim(); index = end; return Double.parseDouble(s); } /** Locate the next comma or right parenthesis. @return The index of the next comma or right parenthesis, or -1 if none is found. */ int commaOrCloseParen() { for(int i = index; i < buffer.length; i++) { if (buffer[i] == ',' || buffer[i] == ')') return i; } return -1; // should never happen } /** Fill the internal character array from the standard input. */ void fillBuffer() { String line = Console.in.readLineNonwhite(); if (line != null) buffer = line.toCharArray(); else buffer = null; index = 0; } }