/** * Title: * Description: * Copyright: Copyright (c) 2001 * Company: * @author Burton Rosenberg * @version 1.0 * * 10 Sept 2003 * */ import java.io.* ; import java.util.* ; public class ReadIntegers { String prompt ; String huhString ; StringTokenizer st = new StringTokenizer("") ; BufferedReader br ; public ReadIntegers(String prompt, String huhString) { this.prompt = prompt ; this.huhString = huhString ; br = new BufferedReader(new InputStreamReader(System.in)) ; } int nextInteger() throws ReadIntegersException { do { try { while ( !st.hasMoreTokens() ) { if ( prompt!=null ) System.out.print(prompt); String s = br.readLine() ; if ( s==null ) throw new ReadIntegersException() ; st = new StringTokenizer(s) ; // empty lines terminate. remove the following // line if this behavior is not wanted if ( !st.hasMoreTokens() ) throw new ReadIntegersException() ; } return Integer.parseInt(st.nextToken()) ; } // catch blocks catch ( NumberFormatException nfs ) { // if huhString != null, complain about bad input // before retrying, else be silent (non-interactive mode). if ( huhString!=null ) System.out.println(huhString) ; } catch ( IOException ioe ) { } ; } while (true) ; } }