0% found this document useful (0 votes)
19 views

ITC C106 Lecture - Command Line Arguments

Uploaded by

Isaac esparrago
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)
19 views

ITC C106 Lecture - Command Line Arguments

Uploaded by

Isaac esparrago
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/ 16

ITC C106 – Computer

Programming II

Command Line Arguments

Prepared by: A. C. Valderama – CSE- IT


Dept.
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
• Learn how to pass arguments in the command
line.
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 an application for one
invocation.

• 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 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.
Command-line Arguments
• Given the previous example where we run:

java Sort 11 33 22 44 35

the arguments are stored in the args array of the


main method declaration. 0
args 11
33 1
22 2
44
3
35
4
Command-line Arguments
• To print the array of arguments, we write:
public class CommandLineSample
{
public static void main( String[] args ){

for(int i=0; i<args.length; i++){


System.out.println( args[i] );
}

}
}
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).
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,
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 the
prompt
• Open the command prompt.
• Windows username usually appears in the
prompt.
Command-line Arguments in the
prompt
• Change to the directory where your Java source
codes are located. Use the command CD for
Change Directory. Type the drive letter in case you
need to change drives.
Command-line Arguments in the
prompt
• Change PATH of the JDK where javac (command
to compile) and java (command to run) are
located.
Command-line Arguments in the
prompt
• Compile and run your source codes.
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.
Resources:
• Adapted and edited from Java Education
and Development Initiative

Prepared by: A. C. Valderama – CSE- IT


Dept.
Thank you!

Prepared by: A. C. Valderama – CSE- IT


Dept.

You might also like