Tutorial3
Tutorial3
Keyboard
1
Objectives
At the end of the lesson, the student should be able to:
2
Getting Input from the
Keyboard
● Two methods of getting input:
- BufferedReader class
- JOptionPane class
● graphical user interface
3
Using BufferedReader Class
● BufferedReader class
- Found in the java.io package
- Used to get input
4
Steps to get Input
1. Add this at the top of your code:
import java.io.*;
5
Steps to get Input
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”);
}
6
Sample Program
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
9 InputStreamReader( System.in) );
10
11 String name = "";
12 System.out.print("Please Enter Your Name:");
13 try{
14 name = dataIn.readLine();
15 }catch( IOException e ){
16 System.out.println("Error!");
17 }
18 System.out.println("Hello " + name +"!");
19 }
20 }
7
Sample Program
● The lines,
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
8
Sample Program
● 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.
9
Sample Program
● The statement,
public class GetInputFromKeyboard {
10
Sample Program
● The statement,
BufferedReader dataIn = new BufferedReader(new InputStreamReader
( System.in) );
11
Sample Program
● The statement,
String name = "";
12
Sample Program
● The given block defines a try-catch block.
try{
name = dataIn.readLine();
}catch( IOException e ){
System.out.println("Error!");
}
name = dataIn.readLine();
will be caught.
- We will cover more about exception handling in the latter part of this
course.
13
Sample Program
● Now going back to the statement,
name = dataIn.readLine();
14
Using JOptionPane Class
● Another way to get input from the user is by using the
JOptionPane class which is found in the javax.swing
package.
15
Sample Program
1 import javax.swing.JOptionPane;
2
3 public class GetInputFromKeyboard {
4
5 public static void main( String[] args ){
6 String name = "";
7 name=JOptionPane.showInputDialog(“Please enter your name");
8 String msg = "Hello " + name + "!";
9 JOptionPane.showMessageDialog(null, msg);
10 }
11}
16
Sample Program Output
17
Sample Program
● The statement,
import javax.swing.JOptionPane;
18
Sample Program
● The statement,
name=JOptionPane.showInputDialog(“Please enter your name");
19
Sample Program
● The statement,
String msg = "Hello " + name + "!";
20
Sample Program
● The statement,
JOptionPane.showMessageDialog(null, msg);
21
Summary
● Discussed two ways of getting input from the user by using
the classes:
- BufferedReader
- JOptionPane
● Brief overview of packages
- Groups related classes in Java
- Classes inside packages can be used by importing the package
22
import java.util.Scanner; //
needed to use Scanner for input
public class KeyboardScanner {
public static void main(String[]
args) {
int num1;
double num2;
String name;
double sum;
// Setup a Scanner called in to scan the keyboard
(System.in)
Scanner in = new Scanner(System.in);
System.out.print("Enter an integer: ");
num1 = in.nextInt(); // use nextInt() to read int
System.out.print("Enter a floating point number:
");
num2 = in.nextDouble(); // use nextDouble() to
read double
System.out.print("Enter your name: ");
name = in.next(); // use next() to read String
• // Display
• ......
•
• // Close the input stream
• in.close();
• }
• }
•