1  /*
  2   *  File:  Console.java
  3   *
  4   *  Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM)
  5   *  Published By SunSoft Press/Prentice-Hall
  6   *  Copyright (C) 1996 Sun Microsystems Inc.
  7   *  All Rights Reserved. ISBN 0-13-565755-5
  8   *
  9   *  Permission to use, copy, modify, and distribute this 
 10   *  software and its documentation for NON-COMMERCIAL purposes
 11   *  and without fee is hereby granted provided that this 
 12   *  copyright notice appears in all copies. 
 13   *  
 14   *  THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR 
 15   *  WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER 
 16   *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
 17   *  IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 18   *  PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS
 19   *  AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED 
 20   *  BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING 
 21   *  THIS SOFTWARE OR ITS DERIVATIVES.
 22   *
 23   *  Modifications by Tom Scavo <trscavo@npac.syr.edu> 10/6/97
 24   *                                                    11/2/97
 25   *
 26   */
 27   
 28  /**
 29   *  Methods to read numbers and strings from standard input,
 30   *  and print prompts to standard output.
 31   *  @version 1.02, 2 Nov 1997
 32   *  @author Cay Horstmann, Tom Scavo
 33   */
 34  
 35  package corejava;
 36  
 37  public class Console {
 38  
 39     /*
 40      *  The Console class may not be instantiated.
 41      */
 42      
 43     private Console() { };
 44  
 45     /**
 46      *  Print a prompt to the console.  Do not print a newline.
 47      *  @param prompt the prompt string to display
 48      */
 49      
 50     public static void printPrompt( String prompt ) {
 51        System.out.print( prompt );
 52        System.out.flush();
 53     }
 54     
 55     /**
 56      *  Read a word from the console. A word is 
 57      *  any set of characters terminated by whitespace.
 58      *  @return the 'word' entered
 59      */
 60      
 61     public static String readWord() {
 62        int ch;
 63        String r = "";
 64        boolean done = false;
 65        while ( !done ) {
 66           try {
 67              ch = System.in.read();
 68              if ( ch < 0 || Character.isWhitespace( (char)ch ) )
 69                 done = true;
 70              else
 71                 r = r + (char)ch;
 72           } catch ( java.io.IOException e ) {
 73              done = true;
 74           }
 75        }
 76        return r;
 77     }
 78  
 79     /**
 80      *  Print a prompt and read a word from the console.
 81      *  A word is any set of characters terminated by whitespace.
 82      *  @param prompt the prompt string to display
 83      *  @return the 'word' entered
 84      */
 85      
 86     public static String readWord( String prompt ) {
 87        printPrompt( prompt );
 88        return readWord();
 89     }
 90  
 91     /**
 92      *  Read a string from the console. The string is 
 93      *  terminated by a newline.
 94      *  @return the input string (without the newline)
 95      */
 96      
 97     public static String readString() {
 98        int ch;
 99        String r = "";
100        boolean done = false;
101        while ( !done ) {
102           try {
103              ch = System.in.read();
104              if ( ch < 0 || (char)ch == '\n' )
105                 done = true;
106              else
107                 r = r + (char)ch;
108           } catch ( java.io.IOException e ) {
109              done = true;
110           }
111        }
112        return r;
113     }
114  
115     /**
116      *  Print a prompt and read a string from the console.
117      *  The string is terminated by a newline.
118      *  @param prompt the prompt string to display
119      *  @return the input string (without the newline)
120      */
121      
122     public static String readString( String prompt ) {
123        printPrompt( prompt );
124        return readString();
125     }
126  
127     /**
128      *  Read an integer from the console.
129      *  The input is terminated by a newline and validated.
130      *  @return the input value as an int
131      *  @exception NumberFormatException if non-integer input
132      */
133      
134     public static int readInt() {
135        String input;
136        input = readString().trim();
137        try {
138           return Integer.valueOf(input).intValue();
139        } catch ( NumberFormatException e ) {
140           throw e;
141        }
142     }
143  
144     /**
145      *  Print a prompt and read an integer from the console.
146      *  The input is terminated by a newline and validated.
147      *  If the input is invalid, an error message is printed
148      *  on stdout and the user is prompted for a new value.
149      *  @param prompt the prompt string to display
150      *  @return the input value as an int
151      */
152      
153     public static int readInt( String prompt ) {
154        String input;
155        while ( true ) {
156           input = readString( prompt ).trim();
157           try {
158              return Integer.valueOf(input).intValue();
159           } catch ( NumberFormatException e ) {
160              System.out.println
161                 ("Not an integer: " + input + "\nPlease try again!");
162           }
163        }
164     }
165  
166     /**
167      *  Read a floating point number from the console.
168      *  The input is terminated by a newline and validated.
169      *  @return the input value as a double
170      *  @exception NumberFormatException if non-float input
171      */
172      
173     public static double readDouble() {
174        String input;
175        input = readString().trim();
176        try {
177           return Double.valueOf(input).doubleValue();
178        } catch ( NumberFormatException e ) {
179           throw e;
180        }
181     }
182  
183     /**
184      *  Print a prompt and read a floating point number from
185      *  the console.  The input is terminated by a newline
186      *  and validated.
187      *  If the input is invalid, an error message is printed
188      *  on stdout and the user is prompted for a new value.
189      *  @param prompt the prompt string to display
190      *  @return the input value as a double
191      */
192      
193     public static double readDouble( String prompt ) {
194        String input;
195        while ( true ) {
196           input = readString( prompt ).trim();
197           try {
198              return Double.valueOf(input).doubleValue();
199           } catch ( NumberFormatException e ) {
200              System.out.println
201                 ("Not a floating point number: " + input + "\nPlease try again!");
202           }
203        }
204     }
205     
206  } // end class Console