KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > Console


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.util;
25
26 /**
27    An easy interface to read numbers and strings from
28    standard input
29
30    @version $Revision: 1.3 $
31    @author bnevins
32 */

33
34 public class Console
35 {
36     /** print a prompt on the console but don't print a newline
37       @param prompt the prompt string to display
38     */

39
40     public static void printPrompt(String JavaDoc prompt)
41     {
42         System.out.print(prompt + " ");
43         System.out.flush();
44     }
45
46     /** read a string from the console. The string is
47         terminated by a newline
48         @return the input string (without the newline)
49     */

50
51     public static String JavaDoc readLine()
52     {
53         int ch;
54         String JavaDoc r = "";
55         boolean done = false;
56         
57         while (!done)
58         {
59             try
60             {
61                 ch = System.in.read();
62                 if (ch < 0 || (char)ch == '\n')
63                     done = true;
64                 
65                 else if ((char)ch != '\r') // weird--it used to do \r\n translation
66
r = r + (char) ch;
67             }
68             catch(java.io.IOException JavaDoc e)
69             {
70                 done = true;
71             }
72         }
73         
74         return r;
75     }
76
77     /** read a string from the console. The string is
78         terminated by a newline
79         @param prompt the prompt string to display
80         @return the input string (without the newline)
81     */

82
83     public static char getKey(String JavaDoc prompt)
84     {
85         printPrompt(prompt);
86         int ch = '\n';
87         
88         try
89         {
90             ch = System.in.read();
91         }
92         catch(java.io.IOException JavaDoc e)
93         {
94         }
95         return (char)ch;
96         
97     }
98     
99     /** read a string from the console. The string is
100         terminated by a newline
101         @param prompt the prompt string to display
102         @return the input string (without the newline)
103     */

104
105     public static String JavaDoc readLine(String JavaDoc prompt)
106     {
107         printPrompt(prompt);
108         return readLine();
109     }
110
111     /** read an integer from the console. The input is
112     terminated by a newline
113     @param prompt the prompt string to display
114     @return the input value as an int
115     @exception NumberFormatException if bad input
116     */

117
118     public static int readInt(String JavaDoc prompt)
119     {
120         while(true)
121         {
122             printPrompt(prompt);
123             
124             try
125             {
126                 return Integer.valueOf
127                 (readLine().trim()).intValue();
128             }
129             catch(NumberFormatException JavaDoc e)
130             {
131                 System.out.println("Not an integer. Please try again!");
132             }
133         }
134     }
135
136     /** read a floating point number from the console.
137     The input is terminated by a newline
138     @param prompt the prompt string to display
139     @return the input value as a double
140     @exception NumberFormatException if bad input
141     */

142
143     public static double readDouble(String JavaDoc prompt)
144     {
145         while(true)
146         {
147             printPrompt(prompt);
148             
149             try
150             {
151                 return Double.parseDouble(readLine().trim());
152             }
153             catch(NumberFormatException JavaDoc e)
154             {
155                 System.out.println("Not a floating point number. Please try again!");
156             }
157         }
158     }
159 }
160
Popular Tags