FileDocCategorySizeDatePackage
Console.javaAPI DocGlassfish v2 API4572Fri May 04 22:32:06 BST 2007com.sun.enterprise.util

Console

public class Console extends Object
An easy interface to read numbers and strings from standard input
version
$Revision: 1.4 $
author
bnevins

Fields Summary
Constructors Summary
Methods Summary
public static chargetKey(java.lang.String prompt)
read a string from the console. The string is terminated by a newline

param
prompt the prompt string to display
return
the input string (without the newline)

  
		printPrompt(prompt);
		int ch = '\n";
		
		try
		{  
			ch = System.in.read();
		}
		catch(java.io.IOException e)
		{  
		}
		return (char)ch;
		
	
public static voidprintPrompt(java.lang.String prompt)
print a prompt on the console but don't print a newline

param
prompt the prompt string to display

  
		System.out.print(prompt + " ");
		System.out.flush();
	
public static doublereadDouble(java.lang.String prompt)
read a floating point number from the console. The input is terminated by a newline

param
prompt the prompt string to display
return
the input value as a double
exception
NumberFormatException if bad input

  
		while(true)
		{  
			printPrompt(prompt);
			
			try
			{  
				return Double.parseDouble(readLine().trim());
			} 
			catch(NumberFormatException e)
			{  
				System.out.println("Not a floating point number. Please try again!");
			}
		}
	
public static intreadInt(java.lang.String prompt)
read an integer from the console. The input is terminated by a newline

param
prompt the prompt string to display
return
the input value as an int
exception
NumberFormatException if bad input

  
		while(true)
		{  
			printPrompt(prompt);
			
			try
			{  
				return Integer.valueOf
				(readLine().trim()).intValue();
			} 
			catch(NumberFormatException e)
			{  
				System.out.println("Not an integer. Please try again!");
			}
		}
	
public static java.lang.StringreadLine()
read a string from the console. The string is terminated by a newline

return
the input string (without the newline)

  
		int ch;
		String r = "";
		boolean done = false;
		
		while (!done)
		{  
			try
			{  
				ch = System.in.read();
				if (ch < 0 || (char)ch == '\n")
					done = true;
				
				else if ((char)ch != '\r") // weird--it used to do \r\n translation
					r = r + (char) ch;
			}
			catch(java.io.IOException e)
			{  
				done = true;
			}
		}
		
		return r;
	
public static java.lang.StringreadLine(java.lang.String prompt)
read a string from the console. The string is terminated by a newline

param
prompt the prompt string to display
return
the input string (without the newline)

  
		printPrompt(prompt);
		return readLine();