SlideShare a Scribd company logo
MODULE 1:
Object-Oriented Program
Logic and Design
Chapter 9: Getting
Program Input
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
• 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
Getting Input Using JOptionPane
• The statement, JOptionPane.showMessageDialog
(null, msg);
displays a dialog box containing the message and an
OK button.

More Related Content

PPT
Introduction to Java Programming
PPTX
Java Programming Fundamentals
PPT
Introduction
PPTX
java: basics, user input, data type, constructor
PDF
C progrmming
PPTX
Chapter 2.1
PPTX
Introduction to java
PPT
Java platform
Introduction to Java Programming
Java Programming Fundamentals
Introduction
java: basics, user input, data type, constructor
C progrmming
Chapter 2.1
Introduction to java
Java platform

What's hot (20)

PPT
Java tutorial PPT
DOCX
Unit of competency
PPT
Ppt chapter07
PPT
Java interface
PPT
Basics of java 1
PPT
Interfaces In Java
PPTX
OCP Java (OCPJP) 8 Exam Quick Reference Card
PPTX
Java programming course for beginners
PPT
01slide
PPT
01slide
PDF
Basic Java Programming
PPT
Java interfaces & abstract classes
PDF
Understanding And Using Reflection
PPT
Pptchapter04
PPTX
Mpl 1
PPTX
Object oriented programming-with_java
PDF
Abap object-oriented-programming-tutorials
PDF
Bt0074 oops with java2
PDF
Java Presentation For Syntax
PPSX
Java & advanced java
Java tutorial PPT
Unit of competency
Ppt chapter07
Java interface
Basics of java 1
Interfaces In Java
OCP Java (OCPJP) 8 Exam Quick Reference Card
Java programming course for beginners
01slide
01slide
Basic Java Programming
Java interfaces & abstract classes
Understanding And Using Reflection
Pptchapter04
Mpl 1
Object oriented programming-with_java
Abap object-oriented-programming-tutorials
Bt0074 oops with java2
Java Presentation For Syntax
Java & advanced java
Ad

Similar to Getting Program Input (20)

PPTX
Java Notes
PPTX
Java Notes by C. Sreedhar, GPREC
PPT
02basics
PPTX
#_ varible function
PPTX
JAVA PROGRAMMING presentation for engineering student.pptx
PPT
JAVA_BASICS.ppt
PDF
Class notes(week 5) on command line arguments
PPTX
Java For beginners and CSIT and IT students
PPT
JAVA_BASICS_Data_abstraction_encapsulation.ppt
PPTX
PPTX
DOCX
Class notes(week 5) on command line arguments
PDF
Java 5 and 6 New Features
PPTX
Introduction to Python.Net
PPT
Command line arguments.21
PDF
Command line-arguments-in-java-tutorial
PPTX
Lab 01hhkhkhkhkhkhkhkhkhjjjkhkkhhhhh.pptx
PPTX
Lab 01jbjbjkbkbjkbkjbkjbkbkjbbnjjkb.pptx
PPTX
Java Programs
Java Notes
Java Notes by C. Sreedhar, GPREC
02basics
#_ varible function
JAVA PROGRAMMING presentation for engineering student.pptx
JAVA_BASICS.ppt
Class notes(week 5) on command line arguments
Java For beginners and CSIT and IT students
JAVA_BASICS_Data_abstraction_encapsulation.ppt
Class notes(week 5) on command line arguments
Java 5 and 6 New Features
Introduction to Python.Net
Command line arguments.21
Command line-arguments-in-java-tutorial
Lab 01hhkhkhkhkhkhkhkhkhjjjkhkkhhhhh.pptx
Lab 01jbjbjkbkbjkbkjbkjbkbkjbbnjjkb.pptx
Java Programs
Ad

More from Dr. Rosemarie Sibbaluca-Guirre (20)

PPTX
Korean Language: Culture 한국어 개요
PPTX
Korean Language Overview 한국어 개요
PPTX
Conjunction 접속사
PPTX
PPTX
Usage of Particles 입자의 사용
PPTX
Usage of Particles 입자의 사용
PPTX
Korean Word Order 한국어 단어 순서
PPTX
Korean Number 한국 번호
PPTX
ISAD 313-3_ TOOLS OF THE SYSTEM ANALYSIS.pptx
PPTX
ISAD 313-1_INTRODUCTION TO SYSTEMS.pptx
PPTX
ISAD 313-2_ SYSTEM ANALYSIS.pptx
PPTX
ISAD 313-4_ RESEARCH PROJECT.pptx
PPTX
ISAD 313-3_ SYSTEM FLOW.pptx
PPTX
ISAD 313-3_ MODELS.pptx
PPTX
ACCT11_9_Financial Position.pptx
PPTX
ACCT11_8_Equity.pptx
PPTX
ACCT11_7_Performance.pptx
PPTX
ACCT11_6_Worksheet.pptx
PPTX
ACCT11_5_Adjusting Entries.pptx
PPTX
ACCT11_4_Trial Balance.pptx
Korean Language: Culture 한국어 개요
Korean Language Overview 한국어 개요
Conjunction 접속사
Usage of Particles 입자의 사용
Usage of Particles 입자의 사용
Korean Word Order 한국어 단어 순서
Korean Number 한국 번호
ISAD 313-3_ TOOLS OF THE SYSTEM ANALYSIS.pptx
ISAD 313-1_INTRODUCTION TO SYSTEMS.pptx
ISAD 313-2_ SYSTEM ANALYSIS.pptx
ISAD 313-4_ RESEARCH PROJECT.pptx
ISAD 313-3_ SYSTEM FLOW.pptx
ISAD 313-3_ MODELS.pptx
ACCT11_9_Financial Position.pptx
ACCT11_8_Equity.pptx
ACCT11_7_Performance.pptx
ACCT11_6_Worksheet.pptx
ACCT11_5_Adjusting Entries.pptx
ACCT11_4_Trial Balance.pptx

Recently uploaded (20)

PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Classroom Observation Tools for Teachers
PDF
RMMM.pdf make it easy to upload and study
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Trump Administration's workforce development strategy
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
master seminar digital applications in india
PDF
01-Introduction-to-Information-Management.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Final Presentation General Medicine 03-08-2024.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Classroom Observation Tools for Teachers
RMMM.pdf make it easy to upload and study
Orientation - ARALprogram of Deped to the Parents.pptx
Complications of Minimal Access Surgery at WLH
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Chinmaya Tiranga quiz Grand Finale.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Trump Administration's workforce development strategy
Supply Chain Operations Speaking Notes -ICLT Program
master seminar digital applications in india
01-Introduction-to-Information-Management.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Final Presentation General Medicine 03-08-2024.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf

Getting Program Input

  • 1. MODULE 1: Object-Oriented Program Logic and Design Chapter 9: Getting Program Input
  • 2. 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
  • 3. 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.
  • 4. 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
  • 5. 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”
  • 6. 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]); }
  • 7. 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
  • 8. 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 }
  • 9. 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”.
  • 10. 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.
  • 11. 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;
  • 12. 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: ”);
  • 13. 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); } }
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. 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.
  • 18. 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
  • 19. 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); } }
  • 20. 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.
  • 21. 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
  • 22. • 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
  • 23. Getting Input Using JOptionPane • The statement, JOptionPane.showMessageDialog (null, msg); displays a dialog box containing the message and an OK button.