Before examining Java’s control statements, we will make a short digression that will allow you to begin writing interactive programs. Up to this point, the sample programs in this book have displayed information to the user, but they have not received information from the user. Thus, you have been using console output, but not console (keyboard) input. The main reason for this is that Java’s input system relies upon a rather complex system of classes, the use of which requires an understanding of various features, such as exception handling and classes, that are not discussed until later in this book. There is no direct parallel to the very convenient println( )method, for example, that allows you to read various types of data entered by the user. Frankly, Java’s approach to console input is not as easy to use as one might like. Also, most real-world Java programs and applets will be graphical and window based, not console based. For these reasons, not much use of console input is found in this book. However, there is one type of console input that is easy to use: reading a character from the keyboard. Since several of the examples in this module will make use of this feature, it is discussed here. The easiest way to read a character from the keyboard is to call System.in.read( ). System.inis the complement to System.out. It is the input object attached to the keyboard. Theread( )method waits until the user presses a key and then returns the result. The character is returned as an integer, so it must be cast in to a char to assign it to a char variable. By default, console input is line buffered, so you must press ENTER before any character that you type will be sent to your program. Here is a program that reads a character from the keyboard:
// Read a character from the keyboard.
class KbIn
{
public static void main(String args[])
throws java.io.IOException
{
Friday, December 14, 2018
Input Characters from the Keyboard
December 14, 2018
No comments
char ch;
System.out.print("Press a key followed by ENTER: ");
ch = (char)
System.in.read();
// get a char
System.out.println("Your key is: " + ch);
}
}
Here is a sample run:
Press a key followed by ENTER: t Your key is: t
In the program, notice that main( )begins like this:
public static void main(String args[])
throws java.io.IOException
{
BecauseSystem.in.read( )
is being used, the program must specify the throws java.io.IOExceptionclause. This line is necessary to handle input errors. It is part of Java’s exception handling mechanism, which is discussed in Module 9. For now, don’t worry about its precise meaning. The fact that System. in is line buffered is a source of annoyance at times. When you press ENTER, a carriage return, line feed sequence is entered in to the input stream. Further more, these characters are left pending in the input buffer until you read them.Thus, for some applications, you may need to remove them(by reading them)before the next input operation. You will see an example of this later in this module.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment