0% found this document useful (0 votes)
17 views21 pages

Session 5 Getting Input

Uploaded by

am hungry
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)
17 views21 pages

Session 5 Getting Input

Uploaded by

am hungry
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
You are on page 1/ 21

Getting Input from the Keyboard

In order to make our programs more interactive, we will get some input from the
user. There are three (3) methods of getting input from keyboard. These are
through the use of the BufferedReader class, the one involves a graphical user
interface by using JOptionPane, and the other one using the Scanner Scan.
• Using BufferedReader to get input
The BufferedReader class which is found in the java.io package will be used in
order to get input from the keyboard.
These are the following steps to get input from the keyboard:
1. Add this at the top of your code:
import java.io.*;
2. Add this statement:
BufferedReader dataIn = new BufferedReader(new
InputStreamReader(System.in) );
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”);
}
Getting Input from the Keyboard
The complete sample source code Using BufferedReader:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class GetInputFromKeyboard
{
public static void main( String[] args )
{
BufferedReader dataIn = new BufferedReader(new
InputStreamReader( System.in) );
String name = "";
System.out.print("Please Enter Your Name:");
Try
{
name = dataIn.readLine();
}
catch( IOException e )
{
System.out.println("Error!");
}
System.out.println("Hello " + name +"!");
}
}
Getting Input from the Keyboard
To explain each line of code:

The statements,

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

indicate that we want to use the classes BufferedReader,


InputStreamReader and IOException which is inside the java.io package.
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. Just like in the example, the java.io package contains classes
that allow programs to input and output data. The statements can also be
rewritten as,

import java.io.*;

which will load all the classes found in the package, and then we can use those
classes inside our program.
Getting Input from the Keyboard
The complete sample source code Using BufferedReader:
import java.io.BufferedReader;
These were already discussed in
import java.io.InputStreamReader;
the previous lesson. This means
import java.io.IOException;
we declare a class named
public class GetInputFromKeyboard
GetInputFromKeyboard and
{
we declare the main method.
public static void main( String[] args )
{
BufferedReader dataIn = new BufferedReader(new
InputStreamReader( System.in) );
String name = "";
System.out.print("Please Enter Your Name:");
Try
{
name = dataIn.readLine();
}
catch( IOException e )
{
System.out.println("Error!");
}
System.out.println("Hello " + name +"!");
}
}
Getting Input from the Keyboard
The complete sample source code Using BufferedReader:
import java.io.BufferedReader;
import java.io.InputStreamReader; In the statement, we are
import java.io.IOException; declaring a variable named
public class GetInputFromKeyboard dataIn with the class type
{ BufferedReader.
public static void main( String[] args )
{
BufferedReader dataIn = new BufferedReader(new
InputStreamReader( System.in) );
String name = "";
System.out.print("Please Enter Your Name:");
Try
{
name = dataIn.readLine();
}
catch( IOException e )
{
System.out.println("Error!");
}
System.out.println("Hello " + name +"!");
}
}
Getting Input from the Keyboard
The complete sample source code Using BufferedReader:
import java.io.BufferedReader; Now, we are declaring a String
import java.io.InputStreamReader; variable with the identifier name.
import java.io.IOException; This is where we will store the input
public class GetInputFromKeyboard of the user. The variable name is
{ initialized to an empty String "". It is
public static void main( String[] args ) always good to initialize your
variables as you declare them.
{
BufferedReader dataIn = new BufferedReader(new
InputStreamReader( System.in) );
String name = "";
System.out.print("Please Enter Your Name:");
Try
{
name = dataIn.readLine();
}
catch( IOException e )
{
System.out.println("Error!");
}
System.out.println("Hello " + name +"!");
}
}
Getting Input from the Keyboard
The complete sample source code Using BufferedReader:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class GetInputFromKeyboard
{
public static void main( String[] args )
{
BufferedReader dataIn = new BufferedReader(new
InputStreamReader( System.in) );
String name = "";
System.out.print("Please Enter Your Name:");
Try The next line just outputs a String on
{ the screen asking for the user's name.
name = dataIn.readLine(); Please Enter Your Name:
}
catch( IOException e )
{
System.out.println("Error!");
}
System.out.println("Hello " + name +"!");
}
}
Getting Input from the Keyboard
The complete sample source code Using BufferedReader:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class GetInputFromKeyboard
{
public static void main( String[] args )
{
BufferedReader dataIn = new BufferedReader(new
InputStreamReader( System.in) );
Defines a try-catch block. This assures
String name = ""; that the possible exceptions that could
System.out.print("Please Enter Your Name:"); occur in the statement,
Try name = dataIn.readLine();
{ will be catched. For now, just take note
name = dataIn.readLine(); that you need to add this code in order
} to use the readLine() method of
BufferedReader to get input from the
catch( IOException e )
user. The method call,
{ dataIn.readLine(), gets input from the
System.out.println("Error!"); user and will return a String value.
} This value will then be saved to our
System.out.println("Hello " + name +"!"); name variable
}
}
Getting Input from the Keyboard
The complete sample source code Using BufferedReader:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class GetInputFromKeyboard
{
public static void main( String[] args )
{
BufferedReader dataIn = new BufferedReader(new
InputStreamReader( System.in) );
String name = "";
System.out.print("Please Enter Your Name:");
Try
{
name = dataIn.readLine();
}
catch( IOException e )
{ final statement to greet the user,
System.out.println("Error!");
Hello " + name + "!"
}
System.out.println("Hello " + name +"!");
}
}
Getting Input from the Keyboard
• Using JOptionPane to get input

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. Given the following code:

import javax.swing.JOptionPane;
public class GetInputFromKeyboard
{
public static void main( String[] args )
{
String name = "";
name = JOptionPane.showInputDialog("Please enter your name");
String msg = "Hello " + name + "!";
JOptionPane.showMessageDialog(null, msg);
}
}
Getting Input from the Keyboard
• Using JOptionPane to get input

Given the following code:

import javax.swing.JOptionPane;
public class GetInputFromKeyboard
{
public static void main( String[] args )
{
String name = "";
name = JOptionPane.showInputDialog("Please enter your name");
String msg = "Hello " + name + "!";
JOptionPane.showMessageDialog(null, msg);
}
} The first statement,
import javax.swing.JOptionPane;
indicates that we want to import the class
JOptionPane from the javax.swing package. We can
also write this as,
import javax.swing.*;
Getting Input from the Keyboard
• Using JOptionPane to get input

Given the following code:

import javax.swing.JOptionPane;
public class GetInputFromKeyboard
{
public static void main( String[] args )
{
String name = "";
name = JOptionPane.showInputDialog("Please enter your name");
String msg = "Hello " + name + "!";
JOptionPane.showMessageDialog(null, msg);
}
}
These were already discussed in the previous lesson. This means
we declare a class named GetInputFromKeyboard and we
declare the main method.
Now, we are declaring a String variable with the identifier name.
This is where we will store the input of the user. The variable
name is initialized to an empty String "".
Getting Input from the Keyboard
• Using JOptionPane to get input

Given the following code:

import javax.swing.JOptionPane;
public class GetInputFromKeyboard
{
public static void main( String[] args )
{
String name = "";
name = JOptionPane.showInputDialog("Please enter your name");
String msg = "Hello " + name + "!";
JOptionPane.showMessageDialog(null, msg);
}
}

The statement,
name = JOptionPane.showInputDialog("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.
Getting Input from the Keyboard
• Using JOptionPane to get input

Given the following code:

import javax.swing.JOptionPane;
public class GetInputFromKeyboard
{
public static void main( String[] args )
{
String name = "";
name = JOptionPane.showInputDialog("Please enter your name");
String msg = "Hello " + name + "!";
JOptionPane.showMessageDialog(null, msg);
}
}
Now we create the welcome message, which we will store in the msg
variable,
String msg = "Hello " + name + "!";
Getting Input from the Keyboard
• Using JOptionPane to get input

Given the following code:

import javax.swing.JOptionPane;
public class GetInputFromKeyboard
{
public static void main( String[] args )
{
String name = "";
name = JOptionPane.showInputDialog("Please enter your name");
String msg = "Hello " + name + "!";
JOptionPane.showMessageDialog(null, msg);
}
}

The next line displays a dialog which contains a message and an OK


button.

JOptionPane.showMessageDialog(null, msg);
Getting Input from the Keyboard
• Using JOptionPane to get input

Output:
Getting Input from the Keyboard
• Using Scanner scan to get Input
The following line creates a Scanner Object that reads from the keyboard:
Scanner scan = new Scanner (System.in)
The new operator creates the Scanner object. Once created, the Scanner
object can be used to invoke various input methods such as,
Answer = scan.nextLine();
The nextLine method reads all of the input until the end of the line is
found. Unless specified otherwise, white spaces is used to separate the
elements (called tokens) of the input. White space includes space
characters, tabs, new line and characters. The nextLine method of the
Scanner class reads the next input token and returns it as String.
Methods such as nextInt and nextDouble read data of particular types.
The Scanner class is part of the java.util class library and must be imported
into a program to be used. Hence, the following statement must be added
at the top of your program:
Import java.util.Scanner;
Getting Input from the Keyboard
• Using Scanner scan to get Input
Sample Program1:
Demonstrates the use of the nextLine method of the Scanner class to read a
string from the user.

Import java.util.Scanner;
public class Echo
{
// Reads a character string from the user and prints it.
public static void main (String [] args)
{
String message;
Scanner scan = new Scanner (System.in);
System.out.println(“Enter a line of text: “);
message = scan.nextLine();
System.out.println(“You entered: ” + message );
}
}
Getting Input from the Keyboard
• Using Scanner scan to get Input
Sample Program1:
Demonstrates the use of the nextLine method of the Scanner class to read a
string from the user.

Output:

Enter a line of text: Good Day! (Sample input data)

You entered: Good Day!


Getting Input from the Keyboard
• Using Scanner scan to get Input
Sample Program2: Demonstrates the use of the Scanner class to read
numeric data.

Import java.util.Scanner;
public class GasMileage
{
// Calculates fuel efficiency based on values entered by the user.
public static void main (String [] args)
{
Int miles;
Double gallons, mpg;

Scanner scan = new Scanner (System.in);


System.out.print(“Enter the number of miles : “);
miles = scan.nextInt();

System.out.print(“Enter the gallons of the fuel used: “);


gallons = scan.nextDouble();
mpg = miles/gallons;
System.out.println(“Miles per gallon: “ + mpg);
}
}
Getting Input from the Keyboard
• Using Scanner scan to get Input
Sample Program2: Demonstrates the use of the Scanner class to read
numeric data.

Output:

Enter the number of miles : 500 (Sample input data)

Enter the gallons of the fuel used: 10 (Sample input data)

Miles per gallon: 50.0 mpg

You might also like