Tuesday, May 26, 2009

Java tutorial source code get input from keyboard

Using BufferedReader to get input

In this section, we will use the BufferedReader class found in the java.io package in order to get input from the keyboard.

Here are the steps to get input from the keyboard:

1. Add this at the top of your code:

import java.io.*;

2. Add this statement:

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

3. Declare a temporary String variable to get the input, and invoke the readLine() method to get input from the keyboard. You have to type it inside a try-catch block.

try{
String temp = dataIn.readLine();
}
catch( IOException e ){
System.out.println(“Error in getting input”);
}

Here is the 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 +"!");
}
}

Now let's try to explain each line of code:

The statements,

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

indicate that we want to use the classes BufferedReader, InputStreamReader and IOException which is inside the java.io package. The Java Application Programming Interface (API) contains hundreds of predefined classes that you can use in your programs. These classes are organized into what we call packages.

Packages contain classes that have related purpose. Just like in our example, the java.io package contains classes that allow programs to input and output data. The statements can also be rewritten as,

import java.io.*;

which will load all the classes found in the package, and then we can use those classes inside our program.
The next two statements,

public class GetInputFromKeyboard
{
public static void main( String[] args ){

were already discussed in the previous lesson. This means we declare a class named GetInputFromKeyboard and we declare the main method.

In the statement,

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

we are declaring a variable named dataIn with the class type BufferedReader. Don't worry about what the syntax means for now. We will cover more about this later in the course.

Now, we are declaring a String variable with the identifier name,

String name = "";

This is where we will store the input of the user. The variable name is initialized to an empty String "". It is always good to initialize your variables as you declare them.

The next line just outputs a String on the screen asking for the user's name.

System.out.print("Please Enter Your Name:");

Now, the following block defines a try-catch block,

try{
name = dataIn.readLine();
}catch( IOException e ){
System.out.println("Error!");
}

This assures that the possible exceptions that could occur in the statement

name = dataIn.readLine();

will be catched. We will cover more about exception handling in the latter part of this course, but for now, just take note that you need to add this code in order to use the readLine() method of BufferedReader to get input from the user.

Now going back to the statement,

name = dataIn.readLine();

the method call, dataIn.readLine(), gets input from the user and will return a String value. This value will then be saved to our name variable, which we will use in our final statement to greet the user,

System.out.println("Hello " + name + "!");

Related Post:

Java programing source code sample printing characters

What is programming?

C programming sample source code: Sum of 2 numbers

Java Programming Features