100% found this document useful (1 vote)
143 views

Notes3 Getting Input From Keyboard

This document discusses two methods for getting input from the keyboard in Java programs: using the BufferedReader class and using the JOptionPane class. It provides code examples for getting a single input using each method and displays the input. It also provides an exercise for getting three words of input from the user and displaying them.

Uploaded by

seniorhigh LIS
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
143 views

Notes3 Getting Input From Keyboard

This document discusses two methods for getting input from the keyboard in Java programs: using the BufferedReader class and using the JOptionPane class. It provides code examples for getting a single input using each method and displays the input. It also provides an exercise for getting three words of input from the user and displaying them.

Uploaded by

seniorhigh LIS
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

JEDI

Getting Input from the Keyboard


1 Objectives
Now that we've studied some basic concepts in Java and we've written some simple programs, let's
make our programs more interactive by getting some input from the user. In this section, we'll be
discussing two methods of getting input, the first one is through the use of the BufferedReader class
and the other one involves a graphical user interface by using JOptionPane.

At the end of the lesson, the student should be able to:


 Create an interactive Java program that gets input from the keyboard

 Use the BufferedReader class to get input from the keyboard using a console
 Use the JOptionPane class to get input from the keyboard using a graphical user interface

2 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();
Introduction to Programming III theboi 1
JEDI
}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();
Introduction to Programming III theboi 2
JEDI
}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 + "!");

3 Using JOptionPane to get input


Another way to get input from the user is by using the JOptionPane class which is found in the
javax.swing package. JOptionPane makes it easy to pop up a standard dialog box that prompts users
for a value or informs them of something.

Given the following code,

import javax.swing.JOptionPane;

public class GetInputFromKeyboard


{

public static void main( String[] args ){


String name = "";
name = JoptionPane.showInputDialog("Please enter your
name");

String msg = "Hello " + name + "!";

JOptionPane.showMessageDialog(null, msg);

}
}

This will output,

Figure 1: Getting Input Using JOptionPane

Introduction to Programming III theboi 3


JEDI

Figure 2: Input florence on the JOptionPane

Figure 3: Showing Message Using JOptionPane


The first statement,

import javax.swing.JOptionPane;

indicates that we want to import the class JOptionPane from the javax.swing package.

We can also write this as,

import javax.swing.*;

The statement,

name = JOptionPane.showInputDialog("Please enter your name");

creates a JOptionPane input dialog, which will display a dialog with a message, a textfield and an OK
button as shown in the figure. This returns a String which we will save in the name variable.

Now we create the welcome message, which we will store in the msg variable,

String msg = "Hello " + name + "!";

The next line displays a dialog which contains a message and an OK button.

JOptionPane.showMessageDialog(null, msg);

4 Exercises
4.1 Last 3 words (BufferedReader version)
Using BufferedReader, ask for three words from the user and output those three words on the screen.
For example,
Enter word1:Goodbye
Enter word2:and
Enter word3:Hello

Goodbye and Hello

4.2 Last 3 words (JOptionPane version)


Using JOptionPane, ask for three words from the user and output those three words on the screen. For
example,

Introduction to Programming III theboi 4


JEDI

Figure 4: First Input

Figure 5: Second Input

Figure 6: Third Input

Figure 7: Show Message

Introduction to Programming III theboi 5

You might also like