Class Console

java.lang.Object
  |
  +--Console

public class Console
extends java.lang.Object

The Console is a simple interface for keyboard input and screen output. This "device" allows a programmer to write code for simple text-mode, user interfaces.

The 'screen' is composed of a 25 row by 80 column matrix of character locations. It is divided into two basic sections. Row 25 is reserved for input. When input is requested using one of the read*() methods, any data entered by the user appears on this line. The input may be optionally preceded by a prompt string.

There is an internal cursor that keeps track of the location, in the first (top) 24 rows where characters may be written. The screen coordiates run from 0 to 24 (for rows) and 0 to 79 (for columns). The normal convention is to indicate the row number first (first parameter in most method calls) but the gotoXY() method reverses this order for historical reasons. A program can format the upper screen area in any way desired, for example to produce menus or text-based graphics.


Field Summary
static int COLS
           
static int ROWS
           
 
Constructor Summary
Console()
          Constructing a Console device creates a screen, 24 rows by 80 columns of text (ASCII characters).
 
Method Summary
 void clearScreen()
          Clears all characters from the screen and prints the cleared screen to the monitor.
 void gotoXY(int x, int y)
          Sets the screen cursor to locations indicated by x and y parameters.
 void initScreen()
          Sets up the screen for display.
static void main(java.lang.String[] args)
           
static void printPrompt(java.lang.String prompt)
          Prints a prompt message to the console input row (row 25).
 void printScreen()
          This method is called to display the current contents of the screen.
 void putChar(char c)
          Puts a character in the location determined by the internal cursor.
 void putCharAt(char c, int row, int col)
          Allows the placement of a single ASCII character at any screen location desired.
 void putString(java.lang.String s)
          Places a String on the screen starting at the current screen cursor location.
 void putStringAt(java.lang.String s, int row, int col)
          Allows the placement of a String at any screen location desired.
static double readDouble(java.lang.String prompt)
          Reads a double value input on the input row (row 25).
static int readInt(java.lang.String prompt)
          Reads an integer value input on the input row (row 25).
static java.lang.String readLine()
          Reads a string input from the console input row (row 25) typed by the user.
static java.lang.String readLine(java.lang.String prompt)
          Similar to readLine() but prints a prompt message on the input row (row 25) prior to waiting for user input.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ROWS

public static final int ROWS

COLS

public static final int COLS
Constructor Detail

Console

public Console()
Constructing a Console device creates a screen, 24 rows by 80 columns of text (ASCII characters). The screen is initially cleared.
Method Detail

initScreen

public void initScreen()
Sets up the screen for display. A special, internal cursor location is set to 0,0 (upper left-hand corner of screen). This cursor will be used to determine the location where the next character will be printed unless one of the put***At() methods is used. In the latter case, the cursor location will be updated to the coordinates immediately following the text that was printed using the put***At() method. See also: gotoXY

printScreen

public void printScreen()
This method is called to display the current contents of the screen. It must be called after using any methods which change the contents of the screen, in order for those changes to show up on the monitor device. For example, one might call the putStringAt() method several times to put formatted text onto the screen, then call printScreen() to display the results on the monitor.
Console c = new Console();

c.putStringAt("Hello World!", 4, 18);
c.putStringAt("How are you?", 5, 18);
c.printScreen();

clearScreen

public void clearScreen()
Clears all characters from the screen and prints the cleared screen to the monitor.

putCharAt

public void putCharAt(char c,
                      int row,
                      int col)
Allows the placement of a single ASCII character at any screen location desired. The character will not show up on the monitor until the next call to printScreen() has occurred.

putChar

public void putChar(char c)
Puts a character in the location determined by the internal cursor. For example if you use the gotoXY() method to place the cursor, and then call this method, the character will be printed at the coordinates specified in the gotoXY() parameters.
c.gotoXY(22, 5);  \\ put cursor at the coordinates 5, 22
putChar('|');      \\

putStringAt

public void putStringAt(java.lang.String s,
                        int row,
                        int col)
Allows the placement of a String at any screen location desired. The String will not show up on the monitor until the next call to printScreen() has occurred. Any string that runs past the 80th column will be continued on the next row from column 0. The screen cursor will be updated accordingly.

putString

public void putString(java.lang.String s)
Places a String on the screen starting at the current screen cursor location. To place a string at a specific location use the gotoXY() method to set the cursor, or use the putStringAt() method.

gotoXY

public void gotoXY(int x,
                   int y)
Sets the screen cursor to locations indicated by x and y parameters. The x parameter is the column number; the y parameter is the row number. Note that the order of these parameters is reversed from the other screen method calls that specify row first. This is due to historical reasons, the gotoXY function is used by a number of different systems and is provided here for consistancy. If the x or y parameters are outside the range of row and column values (0 to 24 and 0 to 79 respectively) the method does not change the screen cursor position. Subsequent calls to put*() methods will not result in correct placement of the char or string arguments.

printPrompt

public static void printPrompt(java.lang.String prompt)
Prints a prompt message to the console input row (row 25). To print a prompt message to some other portion of the screen you will need to use the putString or putStringAt methods.

readLine

public static java.lang.String readLine()
Reads a string input from the console input row (row 25) typed by the user. This version does not print a preceding prompt message.

readLine

public static java.lang.String readLine(java.lang.String prompt)
Similar to readLine() but prints a prompt message on the input row (row 25) prior to waiting for user input.

readInt

public static int readInt(java.lang.String prompt)
Reads an integer value input on the input row (row 25). If anything other than an integer is entered, the method notifies the user of the error and waits for an integer to be entered.

readDouble

public static double readDouble(java.lang.String prompt)
Reads a double value input on the input row (row 25). If anything other than a double or an integer is entered, the method notifies the user of the error and waits for a number to be entered.

main

public static void main(java.lang.String[] args)