Tuesday, October 27, 2009

Regression Mrqmin Non Linear

keyboard data from the keyboard in Java (Second form)

Now let's see how to read data from the keyboard in a console application / terminal with the help of the class reading standard Java.
System
class, package java.lang , contains many classes that work at the system level, such as data input or output standard output error messages, access to external factors or system environment variables, among others. We, for the moment we are interested in the "in" System class that represents us the standard input stream of Java. Typically this data input stream from the keyboard or reading some other source is specified as a text file or an element in the network. In the field has the form (signature):

public static final InputStream in

Which tells us that the field is static and can not be instantiated and returns an input stream of data to be called. But how we read the data from the keyboard?

If we had in us back a stream of bytes from a particular source (in this case remember that is the keyboard), then we use the method System.in. read (), which returns the integer value of the next byte in the flow of information is given in reading and range from 0 to 255. All we do is control the reading, since System.in . read () will return only a single value.

To achieve This method can get into a cycle and is interrupted once we have introduced an escape character, such as Enter. And more aesthetic yet, we can use it inside a character reading function:

public static String read () throws java.io.IOException {
; char character;
String value = "";
do {
; character = (char ) System.in. read ();
character value + = value;
}
while (character! = '\\ n' )
return value;
}

The above method reads data from keyboard until the user presses the Enter key. What is happening is that the method read () returns us ascii value of key pressed and what we do is represent their character equivalent.

important thing! System.in. read () throws a or exception IOException input / output data, which will prevent errors in reading at the moment does not control this exception and let the virtual machine directly controls, so we indicate the method of reading to launch it into the top level, ie where it is used. Obviously, if we import the java.io package not necessary to include the entire class path IOException.

0 comments:

Post a Comment