Anda di halaman 1dari 22

Introduction to Programming 1 1

5 Getting Input from


Keyboard
Introduction to Programming 1 2
Objectives
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 JOptionane class to get input from the keyboard


using a graphical user interface
Introduction to Programming 1 3
Getting Input from the
Keyboard

!"o methods of getting input:

BufferedReader class

JOptionane class

graphical user interface


Introduction to Programming 1 4
Using BufferedReader Class

BufferedReader class

#ound in the $ava%io package

Used to get input


Introduction to Programming 1 5
teps to get Input
&% Add this at the top of your code:
import java.io.*;
'% Add this statement:
BufferedReader dataIn = new BufferedReader( new
InputStreamReader( System.in) );
Introduction to Programming 1 6
teps to get Input
(% )eclare a temporary *tring variable to get the input, and
invoke the read+ine,- method to get input from the
keyboard% .ou have to type it inside a try/catch block%
try{
String temp = dataIn.readLine();
}catch( IOException e ){
System.out.println(Error in getting input);
}
Introduction to Programming 1 7
ample !rogram
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 }
Introduction to Programming 1 8
ample !rogram
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

!he lines,
indicate that "e "ant to use the classes BufferedReader,
0nput*treamReader and 0O12ception "hich are inside the
$ava%io package%

!hese statements can also be "ritten as,


import java.io.*;
Introduction to Programming 1 9
ample !rogram

!he Java Application rogramming 0nterface ,A0- contains


hundreds of predefined classes that you can use in your
programs% !hese classes are organi3ed into "hat "e call
packages%

ackages contain classes that have related purpose%


Introduction to Programming 1 10
ample !rogram
public class GetInputFromKeyboard {

public static void main( String[] args ){

!he statement,
means "e declare a class named 4et0nput#rom5eyboard

!he ne2t statement declares the main method%


Introduction to Programming 1 11
ample !rogram
BufferedReader dataIn = new BufferedReader(new InputStreamReader
( System.in) );

!he statement,
declares a variable named dataIn, "ith the class type
BufferedReader%

)on6t "orry about "hat the synta2 means for no"% 7e "ill cover more about
classes and declaring classes later in the course%
Introduction to Programming 1 12
ample !rogram
String name = "";
System.out.print("Please Enter Your Name:");

!he statement,
declares a String variable "ith identifier name.

!he ne2t statement,


outputs a *tring on the screen asking for the user6s name
Introduction to Programming 1 13
ample !rogram
try{
name = dataIn.readLine();
}catch( IOException e ){
System.out.println("Error!");
}

!he given block defines a try/catch block%


!his assures that the possible e2ceptions that could occur in
the statement
name = dataIn.readLine();
"ill be catched%

7e "ill cover more about e2ception handling in the latter part of this
course%
Introduction to Programming 1 14
ample !rogram

8o" going back to the statement,


the method call, data0n%read+ine,-, gets input from the user
and "ill return a *tring value%

!his value "ill then be saved to our name variable, "hich "e
"ill use in our final statement to greet the user,
name = dataIn.readLine();
System.out.println("Hello " + name + "!");
Introduction to Programming 1 15
Using "option!ane Class

Another "ay to get input from the user is by using the


JOptionane class "hich is found in the $ava2%s"ing
package%

JOptionane makes it easy to pop up a standard dialog bo2


that prompts users for a value or informs them of something%
Introduction to Programming 1 16
ample !rogram
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}
Introduction to Programming 1 17
ample !rogram Output
Introduction to Programming 1 18
ample !rogram
import javax.swing.JOptionPane;

!he statement,
indicates that "e "ant to import the class JOptionane from
the $ava2%s"ing package%

!his can also "ritten as,


import javax.swing.*;
Introduction to Programming 1 19
ample !rogram
name=JoptionPane.showInputDialog(Please enter your name");

!he statement,
creates a JOptionane input dialog, "hich "ill display a
dialog "ith a message, a te2tfield and an O5 button as
sho"n in the figure%

!his returns a *tring "hich "e "ill save in the name


variable%
Introduction to Programming 1 20
ample !rogram
String msg = "Hello " + name + "!";

!he statement,
creates the "elcome message, "hich "e "ill store in the
msg variable%
Introduction to Programming 1 21
ample !rogram
JOptionPane.showMessageDialog(null, msg);

!he statement,
displays a dialog "hich contains a message and an O5
button%
Introduction to Programming 1 22
ummary

)iscussed t"o "ays of getting input from the user by using


the classes:

BufferedReader

JOptionane

Brief overvie" of packages

4roups related classes in Java

Classes inside packages can be used by importing the package

Anda mungkin juga menyukai