INPUT - Read from the Keyboard via a Dialog box

Reading data into a program is NOT very easy in Java if you are just starting the language. Consequently I have written an input library to make this aspect of java easier for those on the first year course. The details of Java's input methods will be discussed in the second year course.

To import the input library you need to type the import instruction
import flanagan.io.*;
at the top of your program.

Entering a double
You may then read in a double value using either of the following commands:
    double doubVal = Db.readDouble("Input the value of doubVal");
or
    double doubVal = Db.readDouble("Input the value of doubVal", 1.2e-3);

The first statement will cause a dialog box

to appear. You type in the number you wish to enter and click on OK. If you make a mistake in typing click on Cancel and start again. The String "Input the value of doubVal" can be replaced by any input message you wish to appear in the dialog box.

The second statement will cause the dialog box

to appear. If you click on OK the value 0.0012 will be returned to doubValue. If you type a number of the 0.0012 the number you type will be returned to doubValue, i.e. the second argument is the default value". Again, the String "Input the value of doubVal" can be replaced by any input message you wish to appear in the dialog box and if you make a mistake in typing click on Cancel and start again.

Entering a float, an int, a long, a char or a boolean
The methods are similar to the one described for a double. The method Db.readDouble is replaced by

Db.readFloat for entering a float;
Db.readInt for entering an int [integer];
Db.readLong for entering a long [long integer];
Db.readChar for entering a char [character];
Db.readBoolean for entering a boolean [true or false];

Entering a line of text
The method Db.readline is used if you wish to enter a line of text. The line of text may contain spaces

Ending your program
If you open a window, and opening a dialog box is opening a window, you must close your program with a specific command. The most convenient way is to make the last line of your program the statement
    Db.endProgram()
This command will check that you do wish to end your program. The command
    System.out.exit(0)
will end your program without any check.

Example Program
Several of these methods are illustrated in the example program:
DbInputExample.java

Further details
Details of the java input library Db may be found on Db class
Details of the Michael Thomas Flanagan's total input library may be found on Input and Output Classes
Details of the Michael Thomas Flanagan's full library may be found on Michael Thomas Flanagan's Java Library


Return to Db Class documentation
Return to Java Langauge - Undergraduate Notes - main page