0% found this document useful (0 votes)
76 views17 pages

Java Input with BufferedReader & JOptionPane

The document discusses two ways to get input from the user in Java: using the BufferedReader class and the JOptionPane class. The BufferedReader class is used to read input from the keyboard within a try-catch block to handle exceptions. The JOptionPane class provides a graphical user interface to pop up dialog boxes for getting input or displaying messages to the user.

Uploaded by

Karl Moral
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views17 pages

Java Input with BufferedReader & JOptionPane

The document discusses two ways to get input from the user in Java: using the BufferedReader class and the JOptionPane class. The BufferedReader class is used to read input from the keyboard within a try-catch block to handle exceptions. The JOptionPane class provides a graphical user interface to pop up dialog boxes for getting input or displaying messages to the user.

Uploaded by

Karl Moral
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

BufferedReader class
2. JOptionPane class
 graphical user interface
 BufferedReader class
 Found in the [Link] package
 Used to get input
1. Add this at the top of your code:
import [Link].*;

2. Add this statement:


BufferedReader dataIn = new BufferedReader( new InputStreamReader(
[Link]) );

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 = [Link]();
}catch( IOException e ){
[Link](“Error in getting input”);
}
1. import [Link];

2. import [Link];

3. import [Link];

4.

5. public class GetInputFromKeyboard {

6.

7. public static void main( String[] args ){

8. BufferedReader dataIn = new BufferedReader(new InputStreamReader( [Link])


);

9.

10. String name = "";

11. [Link]("Please Enter Your Name:");

12. try{

13. name = [Link]();

14. }catch( IOException e ){

15. [Link]("Error!");

16. }

17. [Link]("Hello " + name +"!");

18. }

19. }
The lines,
 import [Link];
 import [Link];
 import [Link];

 indicate that we want to use the classes BufferedReader,


InputStreamReader and IOException which are inside
the [Link] package.

These statements can also be written as,


 import [Link].*;
 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( [Link]) );
 declares a variable named dataIn, with the class type
BufferedReader.
The statement,
 String name = "";
 declares a String variable with identifier name.
The next statement,
 [Link]("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 = [Link]();
}catch( IOException e ){
[Link]("Error!");
}
 This assures that the possible exceptions that could occur
in the statement name = [Link](); will be
catched.
Now going back to the statement,
 name = [Link]();

 the method call, [Link](), 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,

[Link]("Hello " + name + "!");


Another way to get input from the user is
by using the JOptionPane class which is
found in the [Link] 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 [Link];
 indicates that we want to import the class JOptionPane from the
[Link] package.
 This can also written as,
 import [Link].*;

 The statement,
 name=[Link](“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,
 [Link](null, msg);
 displays a dialog which contains a message and an OK button.

You might also like