Lesson 1.4a -
Keyboard Input
Purpose: To
learn how to input data into variables using the EasyReader class methods
Program Input Displaying information is only part of the programming process. The part that typically comes before that is input. In Java, there are many ways to perform console or keyboard input. This is typically done by way of a class. In our case, we will use the EasyReader class. The EasyReader class has methods which provide for the ability to input a value from the keyboard, then store that value into an appropriate variable. This could be an int, a double, even a String. Below is a table of the EasyReader input methods.
EasyReader Class Input Methods
| Input Type |
Method Name |
Example of use in code |
int |
|
int number = keybd.readInt(); |
double |
readDouble(); |
double value = keybd.readDouble(); |
char |
readChar(); |
char letter = keybd.readChar(); |
String (single word) |
readWord(); |
String word = keybd.readWord(); |
String (multiple words) |
readLine(); |
String sentence = keybd.readLine(); |
Take note that the word following read in each method is to be capitalized, in keeping with Java conventions. Also recognize that there are two methods for reading a String. The readWord() method will not read beyond a single word and stops reading once it sees a space. So be sure to use readLine() when a string may contain spaces.