Sie sind auf Seite 1von 5

35 | P a g e

V. USER INPUT 
 
Objectives 
 
Programming can be more productive having interaction from the user. In this chapter, we will 
be discussing several methods to asked input from the user. The first one is the bufferedReader, 
the Scanner and the JOptionPane. 
     
    At the end of the chapter, the student should be able to: 
 Create an interactive program that will be ask input from the user  
 Getting input from the keyboard using BufferedReader through console 
 Getting input from the keyboard using Scanner through console 
 Getting input from the keyboard using JOptionPane through a graphical user interface 
 
 
Getting input from the keyboard 
 
When  the  program  gets  input  from  the  keyboard  implies  that  the  user  is  acting  interactively. 
We have three ways to ask input. 
 
Buffered Reader 
In this section, we will use the BufferedReader class found in the java.io package in order 
to  get  input  from  the  keyboard.  It  creates  a  buffering  character‐input  stream  that  uses  a 
default‐sized input buffer. 
 
Using bufferedReader class, first we import the bufferReader class out of the java.io package by 
having this statement: 
 
import java.io.bufferedReader;

or the other way around  
 
import java.io.*;
 
Which loads all classes found in the io package to have access and use those classes inside our 
program. 
 
Then we add the statement,         
 
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
 
Here we are declaring a variable named br with the class type BufferedReader. Since, System.in 
extracts data in the form of bytes from the input stream we wrap it with a BufferedReader to 
decodes them into character stream. A line at a time is read using the readLine(); method. 
 
Training-workshop on Object-oriented Programming using Java
36 | P a g e

Here is the complete code: 
 
import java.io.*;
public class bufferedreader1 {
public static void main(String[] args) {

BufferedReader dataIn = new BufferedReader(new


InputStreamReader(System.in));
String msg=" ";
try {
System.out.print("Enter first and last name: ");
msg = dataIn.readLine();

System.out.print("Hello" +" " + msg +"!");


}

catch (IOException e) {
System.out.print("error");
}
}
}
 
 
Scanner 
Another method of getting input from the keyboard is by using Scanner class which can 
be found in java.util package. Objects of type Scanner are useful for breaking down formatted 
input  into  tokens  and  translating  individual  tokens  according  to  their  data  type.  By  default,  a 
scanner uses white space to separate tokens. 
 
Consider the given program,  
 
We start by importing the class Scanner which is contained in the java.util package so we can 
have access to it. 
 
import java.util.Scanner;
 
Here in the statement   
 
Scanner sc = new Scanner(System.in);

What happened is that we create an input stream object sc and associate it with the standard 
input device. Remember that scanner is predefined java class. 
Now let’s evaluate this statement  
 
System.out.println(sc.nextLine());
 
In  here,  we  retrieve  the  next  input  as  a  string  until  the  end  of  the  line  and  the  value  of  the 
expression  is  the  next  input  line.  As  it  scans  for  the  next  input  it  skips  for  any  whitespace 
character.  
Training-workshop on Object-oriented Programming using Java
37 | P a g e

 
Here is the complete code: 
 
import java.util.Scanner;
public class scanner1 {
public static void main(String[] args) {
System.out.print("Enter name: ");
Scanner sc = new Scanner(System.in);
System.out.print(sc.nextLine());
}
}
 
 
JOptionPane 
 
One  way  of  to  get  input  from  the  user  is  by  using  the  JOptionPane  class  which  is  found  in 
javax.swing package. JOptionPane provides more interactive prepackaged dialog boxes both for 
input  and  output.  Invoking  JOption  method,  the  input  dialogs  (showInputMessage)  and 
message dialogs (showMessageDialog) are displayed. 
 
Syntax of showInputMessage: 
 
JOptiopane.showInputMessage(strExpression);
 
Syntax of showMessageDialog: 
 
JOptiopane.showInputMessage(parentComponet,
messageStringExpression, boxTitle,messageType);
 
 
Consider the following program: 
 
import javax.swing.*;
public class joptionpane
{
public static void main( String[] args )
{
String name = " ";
name = JOptionPane.showInputDialog("Enter Name: ");
JOptionPane.showMessageDialog(null,"Hello " + name + "!");
}
}
 
 
 
 
Training-workshop on Object-oriented Programming using Java
38 | P a g e

Having the first statement  import javax.swing.*; 


 
This  is  an  indication  that  the  class  JOptionPane  will  be  use  and  will  be  imported  from  the 
javax.swing package.  
 
In  the  next  statement,  it  obtain  users  input  from  JOptionPane  input  dialogs  and  stores  the 
string in the variable name. Displayed in the input dialog are textfield where in the user entered 
the data and an OK button to submit the string to the program.  
 
name=JOptionPane.showInputDialog("Enter first and last name: ");
 
 
 
 
 
 
 
Figure 5.0: An Example of a Java input dialog 
 
The next statement display result in a JOptionPane message dialog. Here the stored string value 
is displayed. 
  JOptionPane.showMessageDialog(null,"Hello " + name +"!");
 
Sample program 2: 
 
import javax.swing.*;
public class joptionpane1
{
public static void main( String[] args )
{
int num=0;
int num1=0;

num = Integer.parseInt (JOptionPane.showInputDialog ("Enter 1st number: "));


num1 = Integer.parseInt (JOptionPane.showInputDialog ("Enter 2nd number: "));
int sum = num + num1;
JOptionPane.showMessageDialog(null,"Sum of two numbers: " +
sum,"Summation",JOptionPane.INFORMATION_MESSAGE);
}
}
 
Notice the parameters inside the message dialog,  
 
(null,"Sum of two numbers: " +
sum,"Summation",JOptionPane.INFORMATION_MESSAGE);

Training-workshop on Object-oriented Programming using Java


39 | P a g e

Null  here  specifies  that  the  program  uses  a  default  component  that  causes  the  dialog  box  to 
appear in the middle of the screen. Remember that null is a reserved word in java. 
 
The  messageString  “Sum  of  two  numbers”  is  evaluated  and  it  appears  in  the  dialog  box. 
“Summation” here is represents the title of the dialog box. 
 Lastly, the messageType  
 
JOptionPane.INFORMATION_MESSAGE is an int value representing the type of the icon that will 
appear in the dialog box. You can use certaion JOptionPane options stated below.  
 
 
 
 
 
 
 
 
Figure 5.1: An Example of a Java display dialog 
 
Below  show  the  option  of  the  class  JOptionPane  that  can  be  used  with  the  parameter 
messageType. 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   
Figure 5.2: JOptionPane messageType parameter 
 
Test YourSelf 
 
Create  a  program  that  will  ask  input  a  number  input  from  the  user.  The  program  will  then 
output the number in words. Input ranges from 1‐10. If number inputted is out of range, display  
"INVALID INPUT". Use any control structure and JOptionPane to display the output. 

Training-workshop on Object-oriented Programming using Java

Das könnte Ihnen auch gefallen