1.
BufferedReader class
2. JOptionPane class
graphical user interface
BufferedReader class
Found in the java.io package
Used to get input
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”);
}
1. import java.io.BufferedReader;
2. import java.io.InputStreamReader;
3. import java.io.IOException;
4.
5. public class GetInputFromKeyboard {
6.
7. public static void main( String[] args ){
8. BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in)
);
9.
10. String name = "";
11. System.out.print("Please Enter Your Name:");
12. try{
13. name = dataIn.readLine();
14. }catch( IOException e ){
15. System.out.println("Error!");
16. }
17. System.out.println("Hello " + name +"!");
18. }
19. }
The lines,
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 are inside
the java.io package.
These statements can also be written as,
import java.io.*;
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.
The statement,
public class GetInputFromKeyboard {
means we declare a class named GetInputFromKeyboard
The next statement declares the main method.
public static void main( String[] args ){
The statement,
BufferedReader dataIn = new BufferedReader(new
InputStreamReader( System.in) );
declares a variable named dataIn, with the class type
BufferedReader.
The statement,
String name = "";
declares a String variable with identifier name.
The next statement,
System.out.print("Please Enter Your Name:");
outputs a String on the screen asking for the user's
name
The given 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.
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 + "!");
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.
The statement,
import javax.swing.JOptionPane;
indicates that we want to import the class JOptionPane from the
javax.swing package.
This can also written 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.
The statement,
String msg = "Hello " + name + "!";
creates the welcome message, which we will store in the msg
variable.
The statement,
JOptionPane.showMessageDialog(null, msg);
displays a dialog which contains a message and an OK button.