Command line arguments.21

16
1 Usage of command-line arguments http:// improvejava.blogspot.in 1
  • date post

    22-Oct-2014
  • Category

    Documents

  • view

    581
  • download

    7

description

 

Transcript of Command line arguments.21

Page 1: Command line arguments.21

1

Usage of command-line arguments

http://improvejava.blogspot.in 1

Page 2: Command line arguments.21

http://improvejava.blogspot.in

Objective

On completion of this period, you would be able to know,

• The usage of command-line arguments

2

Page 3: Command line arguments.21

3

Recap

• In the last class we discussed• The String class• Constructors of String class• Methods of String class

http://improvejava.blogspot.in 3

Page 4: Command line arguments.21

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

Page 5: Command line arguments.21

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

Page 6: Command line arguments.21

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

Page 7: Command line arguments.21

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

Page 8: Command line arguments.21

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

Page 9: Command line arguments.21

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

Page 10: Command line arguments.21

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

Page 11: Command line arguments.21

• 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

Page 12: Command line arguments.21

• About command line arguments• Usage of command line arguments

12

Summary

http://improvejava.blogspot.in 12

Page 13: Command line arguments.21

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

Page 14: Command line arguments.21

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

Page 15: Command line arguments.21

1. Explain the use of Command-line arguments with an example program?

15

Frequently Asked Questions

http://improvejava.blogspot.in 15

Page 16: Command line arguments.21

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