// the purpose of this class is to provide methods for the input
// needed for the TC1101 simulator
// note that parseInt does not allow a leading +, e.g. +123 is no good.
import java.io.*;
import java.util.*; // needed for StringTokenizer
class SimIO {
private static final BufferedReader keyboard =
new BufferedReader( new InputStreamReader(System.in));
private static String currentFilename = “” ;
private static BufferedReader current = keyboard ;
public static String readLine() throws IOException {
return current.readLine() ;
}
public static boolean eof() throws IOException {
return !current.ready() ;
}
public static void setInputFile(String filename) throws IOException {
if (currentFilename.equals(filename)) {
; // do nothing
} else {
currentFilename = filename ;
current = new BufferedReader(
new InputStreamReader(
new FileInputStream(filename) ) ) ;
}
}
public static int[] readCommentedIntegerLine() throws IOException {
String inputLine = current.readLine() ;
if ( (inputLine.length() > 0) && (inputLine.charAt(0) != ‘ ‘) ) {
// strip off the comment that starts the line
int commentEnd = lastCommentPosition(inputLine) ;
if (commentEnd == -1) { // the whole line is a (unterminated) comment
inputLine = “”;
} else {
inputLine = inputLine.substring( 1+commentEnd ) ;
}
}
// convert the remaining string to a token list
StringTokenizer tokens = new StringTokenizer(inputLine) ;
// convert the token list to an integer array
int[] values = new int[tokens.countTokens()] ;
for (int i = 0 ; i < values.length ; i++ )
values[i] = Integer.parseInt (tokens.nextToken()) ;
return values ;
}
private static int lastCommentPosition( String s ) {
int last = -1;
int i = 0;
char c = s.charAt( i );
i = s.indexOf( c, i+1 );
while ( i != -1 ) {
last = i;
i = s.indexOf( c,i+1 );
}
return last;
}
}