You are on page 1of 22

AY 2019-2020

COMPUTER
PROGRAMMING 1
Prepared By:

Prof. Jomariss B. Plan, MSIT, SMRIIT


CCS Faculty
Week 5: INPUT DATA
Learning Outcomes
At the end of the topic session, the students
should be able to:

 Explain and apply the different methods in


getting user inputs.
 Create sample programs using input data
methods.
GETTING INPUT FROM KEYBOARD

Three methods of getting input:


 BufferedReader class
 Scanner class
 JOptionPane class
 graphical user interface
Input Data using 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:


public static InputStreamReader reader= new
InputStreamReader (System.in);
public static BufferedReader input= new
BufferedReader (reader);
Or
BufferedReader dataIn = new
BufferedReader( new
InputStreamReader( System.in) );
Input Data using Scanner
The Scanner class is found in the java.util package.
import java.util.*; // so you can use Scanner

Constructing a Scanner object to read console input:


Scanner objectName = new Scanner(System.in);

Example:
Scanner input = new Scanner(System.in);
Method Description

nextInt() reads an int from the user and returns it

nextDouble() reads a double from the user

next() reads a one-word String from the user

nextLine() reads a one-line String from the user


Input Data using Scanner
Each method waits until the user presses
Enter/Space.

int age = 0;
System.out.print("How old are you? ");
age = input.nextInt();
System.out.println("You typed " + age);
Input Data using JOptionPane
• 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 following statement must be before the
program’s class header:
import javax.swing.JOptionPane;
Input Data using JOptionPane
• A dialog box is a small graphical window that
displays a message to the user or requests input.
• A variety of dialog boxes can be displayed using the
JOptionPane class.
• Two of the dialog boxes are:
• Message Dialog - a dialog box that displays a
message.
• Input Dialog - a dialog box that prompts the user
for input.
Input Data using JOptionPane
The JOptionPane class provides static methods to
display each type of dialog box.
Input Data using JOptionPane
JOptionPane.showMessageDialog method is
used to display a message dialog.

JOptionPane.showMessageDialog(null,"Hello
World”);
The second argument is the message that is to
be displayed
Input Data using JOptionPane
• An input dialog is a quick and simple way to
ask the user to enter data.
• The dialog displays a text field, an Ok button
and a Cancel button.
• If Ok is pressed, the dialog returns the user’s
input.
• If Cancel is pressed, the dialog returns null.
Input Data using JOptionPane
String name;
name = JOptionPane.showInputDialog("Enter your name.");
• The argument passed to the method is the message to
display.
• If the user clicks on the OK button, name references the
string entered by the user.
• If the user clicks on the Cancel button, name references null

You might also like