1  /*

  2   * Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM)

  3   * Published By SunSoft Press/Prentice-Hall

  4   * Copyright (C) 1996 Sun Microsystems Inc.

  5   * All Rights Reserved. ISBN 0-13-565755-5

  6   *

  7   * Permission to use, copy, modify, and distribute this 

  8   * software and its documentation for NON-COMMERCIAL purposes

  9   * and without fee is hereby granted provided that this 

 10   * copyright notice appears in all copies. 

 11   * 

 12   * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR 

 13   * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER 

 14   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 

 15   * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 

 16   * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS

 17   * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED 

 18   * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING 

 19   * THIS SOFTWARE OR ITS DERIVATIVES.

 20   */

 21   

 22   /**

 23   * An easy interface to read numbers and strings from 

 24   * standard input

 25   * @version 1.01 15 Feb 1996 

 26   * @author Cay Horstmann

 27   */

 28  

 29  

 30  public class Console

 31  {  /**

 32      * print a prompt on the console but don't print a newline

 33      * @param prompt the prompt string to display

 34      */

 35  

 36     public static void printPrompt(String prompt)

 37     {  System.out.print(prompt + " ");

 38        System.out.flush();

 39     }

 40     

 41     /**

 42      * read a string from the console. The string is 

 43      * terminated by a newline

 44      * @return the input string (without the newline)

 45      */

 46      

 47     public static String readString()

 48     {  int ch;

 49        String r = "";

 50        boolean done = false;

 51        while (!done)

 52        {  try

 53           {  ch = System.in.read();

 54              if (ch < 0 || (char)ch == '\n')

 55                 done = true;

 56              else

 57                 r = r + (char) ch;

 58           }

 59           catch(java.io.IOException e)

 60           {  done = true;

 61           }

 62        }

 63        return r;

 64     }

 65  

 66     /**

 67      * read a string from the console. The string is 

 68      * terminated by a newline

 69      * @param prompt the prompt string to display

 70      * @return the input string (without the newline)

 71      */

 72      

 73     public static String readString(String prompt)

 74     {  printPrompt(prompt);

 75        return readString();

 76     }

 77  

 78     /**

 79      * read a word from the console. The word is 

 80      * any set of characters terminated by whitespace

 81      * @return the 'word' entered

 82      */

 83      

 84     public static String readWord()

 85     {  int ch;

 86        String r = "";

 87        boolean done = false;

 88        while (!done)

 89        {  try

 90           {  ch = System.in.read();

 91              if (ch < 0 

 92                 || java.lang.Character.isWhitespace((char)ch))

 93                 done = true;

 94              else

 95                 r = r + (char) ch;

 96           }

 97           catch(java.io.IOException e)

 98           {  done = true;

 99           }

100        }

101        return r;

102     }

103  

104     /**

105      * read an integer from the console. The input is 

106      * terminated by a newline

107      * @param prompt the prompt string to display

108      * @return the input value as an int

109      * @exception NumberFormatException if bad input

110      */

111      

112     public static int readInt(String prompt)

113     {  while(true)

114        {  printPrompt(prompt);

115           try

116           {  return Integer.valueOf

117                 (readString().trim()).intValue();

118           } catch(NumberFormatException e)

119           {  System.out.println

120                 ("Not an integer. Please try again!");

121           }

122        }

123     }

124  

125     /**

126      * read a floating point number from the console. 

127      * The input is terminated by a newline

128      * @param prompt the prompt string to display

129      * @return the input value as a double

130      * @exception NumberFormatException if bad input

131      */

132      

133     public static double readDouble(String prompt)

134     {  while(true)

135        {  printPrompt(prompt);

136           try

137           {  return Double.valueOf

138                 (readString().trim()).doubleValue();

139           } catch(NumberFormatException e)

140           {  System.out.println

141           ("Not a floating point number. Please try again!");

142           }

143        }

144     }

145  }