You are on page 1of 23

IS OOP 223:

Object-Oriented Program
Logic and Design

Chapter 9: Getting
Program Input
SY 2019-2020 | SECOND SEMESTER | FOR MARCH 16 – 31, 2020
Getting Program Input
• Programs get data or inputs from external
sources: from users or from files. Often,
programs interact with users by getting input
from the keyboard.
• Another way to pass data to programs is
through the command line when the java
command is issued.
• This chapter explains getting input from these
sources:
❑ From the command line
❑ From the keyboard
Command Line Arguments
• A java application can accept any number of
arguments from the command line.
• Command-line arguments allow the user to
affect the operation of a program when it is
invoked.
• The user enters arguments when invoking
the program by specifying them after the
name of the class to be run.
Command Line Arguments
• For example, a java application program
called “AverageGrade” takes in three
numbers and prints out the average grade.
• To run this program you enter the following
on the MSDos command line (for Windows)
or in the unix command line:
• Java AverageGrade 90 60 80
• Note: The arguments are separated by
spaces
Command Line Arguments
• The inputs supplied with the java command are passed
as an argument to the main method of the class.

• The main method


public static void main (String [] args) accepts the
arguments and stores it in an array of String named args.

• The arguments get stored in the args array as:


• args *0+ = “90”
• args *1+ = “60”
• args *2+ = “80”
Command Line Arguments
• Remember that args is an array of Strings,
therefore, numbers passed to the program from
the command-line will be stored as Strings.
• For numbers to be usable, the parseInt method of
the Integer class can be used to convert a String
argument to an integer:
int firstArg = 0;
if (args.length > 0) {
firstArg = Integer.parseInt(args[0]);
}
Command Line Arguments
• Before using command line
arguments, always check the number
of arguments before accessing the
array elements.

• Accessing an element beyond what


was given will generate an exception
or an error.
• Command
Command Line Arguments
• For example, if a program needs 5 inputs from the user,
the following code will check if 5 arguments have been
provided:

If (args.length != 5) { System.out.println(“Invalid
number of arguments”);
System.out.println(“Please enter 5 arguments”);
}
else {
// some statements here
}
Command Line Arguments in NetBeans
• • To pass command-line arguments in NetBeans:
• 1. In the main menu, click on “Run” then select “Set Project
Configuration”. In the sub-menu, select “Customize”.
Command Line Arguments in NetBeans
• Passing Command-Line Arguments in NetBeans
2. Enter the data to be passed to the program in the input box
labeled “Arguments”. Click the “OK” button at the bottom.
3. Run the program.
Getting Input Using BufferedReader
• User inputs can be passed to a program by using
java’s Reader classes. One such class is the
BufferedReader class.
• The BufferedReader class is found in the java.io
package. To use the class and its methods, the
following packages must be imported:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
Getting Input Using BufferedReader
• Program code using BufferedReader:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class GetKeyboardInput {
public static void main (String [] args ) {
BufferedReader dataIn = new BufferedReader (new
InputStreamReader(System.in));
String name = “”;
System.out.print (“Please enter your name: ”);
Getting Input Using BufferedReader
• Program code using BufferedReader – cont’d

try {
name = dataIn.readLine();
} catch (IOException e){
System.out.println(“IO Error”);
}
System.out.println(“Hello ” + name);
}
}
Getting Input Using BufferedReader
• Explanation of Program code:

• Importing the java.io package is necessary


to be able to use the classes in the package:
BufferedReader and InputStreamReader.
The class IOException handles IO exceptions or
errors that may occur during runtime.
Getting
Getting Input Using BufferedReader
• The statement
BufferedReader dataIn =
new
BufferedReader(newInputStreamReader(System.in));

- declares and creates an object dataIn which


is of type BufferedReader. dataIn is “chained” to
another object of type InputStreamReader, which,
in turn, reads the s tandard input device of
System.in
Getting Input Using BufferedReader
• The statement(s)
try {
name = dataIn.readLine();
} catch (IOException e) {
System.out.println(“IO Error”);
}

- The try and catch blocks are necessary in case IO


errors occur during program execution
Getting Input Using BufferedReader
- In the try block, the statement: name =
dataIn.readLine();

reads a line from the object dataIn (which is connected


to the standard input device, System.in) and stores it in
the String name.
The statement
System.out.println(“Hello ” + name);
prints the String, name.
Getting Input 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 dialog box that prompts the user for a
value or informs them of something.
Getting
Getting Input Using JOptionPane
• Program code using JOptionPane:
import javax.swing.JOptionPane;
public class PopUpInput {
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 Using JOptionPane
• Explanation of Program code:

• The statement
import javax.swing.JOptionPane;

is necessary to be able to use the


JOptionPane class and its methods in the
javax.swing package.
Getting Input Using JOptionPane
• In the statement name =
JOptionPane.showInputDialog(“Please enter your name: ”);
• The showInputDialog method of the JOptionPane class
displays a dialog box with a message, a textfield and an OK
button like the one shown below.
Getting
Getting Input Using JOptionPane
• When the user keys in a value in the
textfield, it is stored in the String variable
name.

• The output message is assembled in the


statement: String msg = “Hello ” + name
+ “!”;
Getting Input Using JOptionPane
• The statement, JOptionPane.showMessageDialog
(null, msg);

displays a dialog box containing the message and an


OK button.

You might also like