Getting Input from Keyboard

Monday, July 6, 2009

Getting Input from Keyboard

In this section, we will use the BufferedReader class in the java.io package to obtain input from the keyboard.
Here are the steps required to get input from the keyboard:
1. Add at the top of your code:
import java.io. *;
2. Add this statement:

BufferedReader dataIn = new BufferedReader (new InputStreamReader (System.in));

3. Declare temporary String variable to get input, and use the ReadLine () to get input from the keyboard. You need them in the try-catch block:
try (
String temp = dataIn.readLine ();
)
catch (IOException e) (
System.out.println ( "Error in getting input");
)
Here is a complete source code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class GetInputFromKeyboard
(
public static void main (String [] args) (
BufferedReader dataIn = new BufferedReader (new InputStreamReader (System.in));
String name = "";
System.out.print ( "Please Enter Your Name:");
try (
name = dataIn.readLine ();
) catch (IOException e) (
System.out.println ( "Error!");
)
System.out.println ( "Hello" + name +"!");
)
)
Next will be explained each line of code:
Statement,
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
explains that we will use the classes BufferedReader, InputStreamReader and IOException which is in the java.io package. Java Application Programming Interface (API) already contain hundreds of classes that can be used for your program. Classes are collected into packages.
Packages have a class that has the function of each other. As in the example above, the java.io package contains classes that enable the program to perform input and output data. Above statement can also be written,
import java.io. *;
which will issue all classes are on the package, and then we can use the classes on our program.

Two further statement,
public class GetInputFromKeyboard
(
public static void main (String [] args) (
we have already learned in the previous chapter. This statement declares that we declare a class named GetInputFromKeyboard and declare our main method.
In the statement,
BufferedReader dataIn = new BufferedReader (new InputStreamReader (System.in));
we declare a variable named dataIn with the class type BufferedReader. Do not be concerned about the purpose of syntax at this time. Kita akan them at the end of the discussion.
Now, we will declare a variable with the String identifier name,
String name = "";
Above statement is a place to store input from the user. Variable name diinisialisasi as empty String "". We should always a variable after the mendeklarasikannya us.
The next line is to give the output string on the screen asking the user.
System.out.print ( "Please Enter Your Name:");
Now, under the block is a try-catch block,
try (
name = dataIn.readLine ();
) catch (IOException e) (
System.out.println ( "Error!");
)
On this line the error occurs it is likely that the statement
name = dataIn.readLine ();
akan arrested. We will discuss about exception handling in the next chapter of this discussion, but for now, you simply note that you need to add the code to use the ReadLine () method from BufferedReader to get input from the user.

Next statement,
name = dataIn.readLine ();
dataIn.readLine call the above method (), get input from the user and provide a String value. This value will be stored in the variable name, we will use the statement to the end user,
System.out.println ( "Hello" + name + "!");







0 comments:

Post a Comment

 
 
 
 
Copyright © tutorialprogram
Using Classic Hafid | Bloggerized by Hadena