Command line arguments.21
-
date post
22-Oct-2014 -
Category
Documents
-
view
594 -
download
7
description
Transcript of Command line arguments.21
1
Usage of command-line arguments
http://improvejava.blogspot.in 1
http://improvejava.blogspot.in
Objective
On completion of this period, you would be able to know,
• The usage of command-line arguments
2
3
Recap
• In the last class we discussed• The String class• Constructors of String class• Methods of String class
http://improvejava.blogspot.in 3
4
• How to pass information into a program when you run it?
• This is accomplished by passing command-line arguments to main()
• A command line argument is the information that directly follows the programs name on the command-line when it is executed
Usage of command-line arguments
http://improvejava.blogspot.in 4
5
• To access command-line arguments inside a java program is quite easy
• They are stored as Strings in the String array passed to main()
• For example, the following program displays all of the command-line arguments that it is called with
Usage of command-line arguments contd..
5
6
Example Program : command-line argumentsclass CommandLine {
public static void main (String args[]) {
for(int i=0; i<args.length; i++) {
System.out. println(“ args [ ”+ i “ ] : ”+args[i]);
} // end of for
} // end of main
}// end of class http://improvejava.blogspot.in 6
7
Example Program : Command-line Arguments contd..
• Try executing the program as shown here
• Java CommandLine this is a test 100 -1
•When you do, you will see the following output:
Output :args[0] : thisargs[1] : isargs[2] : aargs[3] : testargs[4] : 100args[5] : -1
7
8
• All command-line arguments are passed as strings
• You must convert it into desired format, using wrapper classes
Usage of command-line arguments
8
9
• Usage of command-line arguments that needs conversion
Example Program
class CommandLine {
public static void main (String args[]) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = a + b;
System .out. println(“ sum of the ”+ a + “ and ” + b+ “ is :”+ c);
} // end of main
}// end of class
9
10
Example Program contd..
• Try executing the program as shown here,
• Java CommandLine 10 20
• When you do, you will see the following output
Output : sum of the 10 and 20 is : 30
http://improvejava.blogspot.in 10
• What are the differences between command line arguments in C and Java ?
11
Discussion
C Java
Number of arguments
One less than the actual number
Equal to actual number
Type of arguments
Character array String
http://improvejava.blogspot.in 11
• About command line arguments• Usage of command line arguments
12
Summary
http://improvejava.blogspot.in 12
1. Command-line arguments are passed from
a. calling method
b. command prompt
c. from JVM
d. from operating system
13
Quiz
http://improvejava.blogspot.in 13
Quiz contd..
2. Command-line arguments are stored in
a. Variables
b. Actual parameters
c. int parameter
d. String array
http://improvejava.blogspot.in 14
1. Explain the use of Command-line arguments with an example program?
15
Frequently Asked Questions
http://improvejava.blogspot.in 15
1. Write a program in Java that takes command-line arguments and display how many number of parameters passed
2. Write a program in Java that takes command line arguments and that searches for a give is present in command-line arguments
3. Write a program in Java that takes command line arguments and print the parameter in String array in reverse order
Assignment
http://improvejava.blogspot.in 16