import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io. IOException; /** The TimeInput class reads input in minutes:seconds format with one time input per line and no blank lines. It is not very robust. Intended usage is to declare one timeInput object and use getNextTime( ) to read the next time input from the user. Obtain the read values of minutes and seconds with getMinutes( ) and getSeconds( ). Note that these values are not checked in any way other than that they are integers (possibly negative) and were separated by a colon(:) in the input. @author Beth Katz, November 2002 */ public class TimeInput { /** builds a new time input object */ public TimeInput( ) { if (in == null) { in = new BufferedReader( new InputStreamReader(System.in) ); } } /** replaces current time input with new value from input */ public void getNextTime( ) { String oneLine; StringTokenizer str; try { oneLine = in.readLine( ); str = new StringTokenizer(oneLine); if (str.countTokens( ) != 1) { throw new IOException("Need one input token on a line"); } else { replace(str.nextToken( )); } } catch(IOException e) { System.err.println("Input/output error:" + e); } } /** replaces current time input with new value from token @param token input string of minutes:seconds */ public void replace(String token) { StringTokenizer timeStr; int min = 0, sec = 0; try { timeStr = new StringTokenizer(token, ":"); min = Integer.parseInt(timeStr.nextToken( )); sec = Integer.parseInt(timeStr.nextToken( )); } catch(NumberFormatException e) { System.err.println("Error: need minutes:seconds"); min = 0; sec = 0; } finally { myMinutes = min; mySeconds = sec; } } /** returns the number of minutes input @return number of minutes */ int getMinutes( ) { return myMinutes; } /** returns the number of seconds input @return number of seconds */ int getSeconds( ) { return mySeconds; } private int myMinutes; private int mySeconds; private static BufferedReader in; }