Monday, May 2, 2011

Input From Keyboard

1. Stream Definition
Java programs to process input / output through the stream. What's that stream? ... Stream is an abstraction that can provide or obtain information. There are two types of streams, ie byte streams and character streams. Byte stream used to provide or store information in a data the form of bytes. For example, to write and read binary files. While the character stream used to process I / O that involves the data in the form of characters. For example, when doing the read or write to a text file. Stream is defined by using four abstract classes, namely: InputStream, OutputStream, Reader, and Writer. Class InputStream, OutputStream is an abstract class that is designed as a parent class (superclass) to classes byte stream which includes categories. While classes Reader and Writer is an abstract class that will be deployed into new classes that include in a stream of characters. Through inheritance (inheritance), all classes derived from InputStream and the Reader will have the method read () is useful for reading the data. While OuputStream and Writer will be used to process data.  
1.1 Byte Stream Byte stream is defined by using two class hierarchies, ie InputStream and OutputStream.
1.2 Character Stream Stream characters are also based on two classes namely Reader and Writer. 2. Perform data input Character, String, and Numeric  
2.1.To handle Input data To handle the input, console input is done through reading against System.in Stream. To get the characters entered through the keyboard into the console screen it is necessary to wrap System.in in in a BufferedReader object. So to create a BufferedReader object to connect with keyboard will require the following code:

BufferedReader br = new BufferedReader (new InputStreamReader (System.in)
 

2.2. Input data in the form of characters Use the method read () contained in the BufferedReader class. Method read is declared as follows:

int read () throws IOException
 

2.3. Input data is a String To read input a string used method readline (). ReadLine method is declared as follows :

String readline () throws IOException  


Java has a JOptionPane class which has static methods to create standard dialogue:

showInputDialog (Object Message)
 

2.4. Numerical data input form To read the input (to input data) is the same in the form of numerical when doing data input a string, then string the input converted to a numeric type. The format of writing a method to read it are:

number = Integer.parseInt (temp);  


number is a variable to store the converted string to integer

No comments:

Post a Comment