KEMBAR78
Command line-arguments-in-java-tutorial | PDF
Command-line
Arguments in Java
Objectives
At the end of the lesson, the student
should be able to:
Know and explain what a command-line
argument is
Get input from the user using command-line
arguments
How to pass command-line arguments in
Command prompt
Command-line Arguments
• Command line arguments means a way to
pass parameters to the main function while
executing the program.
• A Java application can accept any number of
arguments from the command-line.
• Command-line arguments allow the user to
affect the operation of an application.
• The user enters command-line arguments
when invoking the application and specifies
them after the name of the class to run.
Command-line Arguments
• For example, suppose you have a Java
application, called Sort, that sorts five
numbers, you run it like this:
● Note: The arguments are separated by spaces.
Command line arguments in DOS
• The following screen image shows Command
line arguments in DOS:
How Java Application Receive
Command-line Arguments
• In Java, when you invoke an application, the
runtime system passes the command-line
arguments to the application‘s main method
via an array of Strings.
public static void main( String[] args )
• Each String in the array contains one of the
command-line arguments.
args[ ] array
• Given the previous example where we run:
java Sort 5 4 3 2 1
• the arguments are stored in the args array of
the main method declaration as…
Example
To print the array of arguments, we write:
1 public class CommandLineSample
2 {
3 public static void main( String[] args ){
4
5 for(int i=0; i<args.length; i++){
6 System.out.println( args[i] );
7 }
8
9 }
10 }
Conversion of Command-line Arguments
• If your program needs to support a numeric
command-line argument, it must convert a String
argument that represents a number, such as "34",
to a number.
• Here's a code snippet that converts a command-
line argument to an integer,
int firstArg = 0;
if (args.length > 0){
firstArg = Integer.parseInt(args[0]);
}
the parseInt() method in the Integer class throws
a NumberFormatException (ERROR) if the format
of args[0] isn't valid (not a number).
Parsing Numeric Command-Line Arguments with
exception handling
• If an application needs to support a numeric command-line argument, it
must convert a String argument that represents a number, such as "34",
to a numeric value. Here is a code snippet that converts a command-line
argument to an int:
parseInt throws a NumberFormatException if the format of args[0]
isn't valid. All of the Number classes — Integer, Float, Double, and so
on — have parseXXX methods that convert a String representing a
number to an object of their type.
Command-line Arguments:
Coding Guidelines
• Before using command-line arguments, always
check the number of arguments before accessing
the array elements so that there will be no
exception generated.
• For example, if your program needs the user to
input 5 arguments,
Examples
• The code snippet shows the switch method
working through command line arguments…
Compiling and executing Switch.java
Another example…
Summary
Command-line arguments
– How to access the arguments
– How to convert String arguments to integer
using Integer.parseInt method
– How to pass command-line arguments in
Command prompt

Command line-arguments-in-java-tutorial

  • 1.
  • 2.
    Objectives At the endof the lesson, the student should be able to: Know and explain what a command-line argument is Get input from the user using command-line arguments How to pass command-line arguments in Command prompt
  • 3.
    Command-line Arguments • Commandline arguments means a way to pass parameters to the main function while executing the program. • A Java application can accept any number of arguments from the command-line. • Command-line arguments allow the user to affect the operation of an application. • The user enters command-line arguments when invoking the application and specifies them after the name of the class to run.
  • 4.
    Command-line Arguments • Forexample, suppose you have a Java application, called Sort, that sorts five numbers, you run it like this: ● Note: The arguments are separated by spaces.
  • 5.
    Command line argumentsin DOS • The following screen image shows Command line arguments in DOS:
  • 6.
    How Java ApplicationReceive Command-line Arguments • In Java, when you invoke an application, the runtime system passes the command-line arguments to the application‘s main method via an array of Strings. public static void main( String[] args ) • Each String in the array contains one of the command-line arguments.
  • 7.
    args[ ] array •Given the previous example where we run: java Sort 5 4 3 2 1 • the arguments are stored in the args array of the main method declaration as…
  • 8.
    Example To print thearray of arguments, we write: 1 public class CommandLineSample 2 { 3 public static void main( String[] args ){ 4 5 for(int i=0; i<args.length; i++){ 6 System.out.println( args[i] ); 7 } 8 9 } 10 }
  • 9.
    Conversion of Command-lineArguments • If your program needs to support a numeric command-line argument, it must convert a String argument that represents a number, such as "34", to a number. • Here's a code snippet that converts a command- line argument to an integer, int firstArg = 0; if (args.length > 0){ firstArg = Integer.parseInt(args[0]); } the parseInt() method in the Integer class throws a NumberFormatException (ERROR) if the format of args[0] isn't valid (not a number).
  • 10.
    Parsing Numeric Command-LineArguments with exception handling • If an application needs to support a numeric command-line argument, it must convert a String argument that represents a number, such as "34", to a numeric value. Here is a code snippet that converts a command-line argument to an int: parseInt throws a NumberFormatException if the format of args[0] isn't valid. All of the Number classes — Integer, Float, Double, and so on — have parseXXX methods that convert a String representing a number to an object of their type.
  • 11.
    Command-line Arguments: Coding Guidelines •Before using command-line arguments, always check the number of arguments before accessing the array elements so that there will be no exception generated. • For example, if your program needs the user to input 5 arguments,
  • 12.
    Examples • The codesnippet shows the switch method working through command line arguments…
  • 13.
  • 14.
  • 16.
    Summary Command-line arguments – Howto access the arguments – How to convert String arguments to integer using Integer.parseInt method – How to pass command-line arguments in Command prompt