Notes3 Getting Input From Keyboard
Notes3 Getting Input From 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
import java.io.*;
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;
try{
name = dataIn.readLine();
Introduction to Programming III theboi 1
JEDI
}catch( IOException e ){
System.out.println("Error!");
}
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.
were already discussed in the previous lesson. This means we declare a class named
GetInputFromKeyboard and we declare the main method.
In the statement,
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.
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.
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.
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,
import javax.swing.JOptionPane;
JOptionPane.showMessageDialog(null, msg);
}
}
import javax.swing.JOptionPane;
indicates that we want to import the class JOptionPane from the javax.swing package.
import javax.swing.*;
The statement,
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,
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