8 Command-Line  Arguments  Introduction to Programming 1   1
ObjectivesAt the end of the lesson, the student should be able to:●   Know and explain what a command-line argument is●   ...
Command-line Arguments●   A Java application can accept any number of arguments    from the command-line.●   Command-line ...
Command-line Arguments●   For example, suppose you have a Java application, called    Sort, that sorts five numbers, you r...
Command-line Arguments●   In Java, when you invoke an application, the runtime system    passes the command-line arguments...
Command-line Arguments●   Given the previous example where we run:       java Sort      5       4       3      2          ...
Command-line Arguments●   To print the array of arguments, we write:     1 public class CommandLineSample     2 {     3   ...
Command-line Arguments●   If your program needs to support a numeric command-line    argument, it must convert a String ar...
Command-line Arguments:       Coding Guidelines●   Before using command-line arguments, always check the    number of argu...
Command-line Arguments in      Netbeans●   Now, how can we pass command-line arguments in    Netbeans?●   Assuming you alr...
Command-line Arguments in      Netbeans●   Click on Projects (encircled below).                    Introduction to Program...
Command-line Arguments in      Netbeans●   Right-click on the CommandLineExample icon, and a popup    menu will appear. Cl...
Command-line Arguments in      Netbeans●   The Project Properties dialog will then appear.                    Introduction...
Command-line Arguments in      Netbeans●   Now, click on Run-> Running Project                   Introduction to Programmi...
Command-line Arguments in      Netbeans●   On the Arguments textbox, type the arguments you want to    pass to your progra...
Command-line Arguments in      Netbeans●   Now try to RUN your program.                   Introduction to Programming 1   16
Summary●   Command-line arguments    –   How to access the arguments    –   How to convert String arguments to integer usi...
Upcoming SlideShare
Loading in …5
×

MELJUN CORTES Jedi slides intro1-chapter08-command line arguments

425
-1

Published on

MELJUN CORTES Jedi slides intro1-chapter08-command line arguments

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
425
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
62
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MELJUN CORTES Jedi slides intro1-chapter08-command line arguments

  1. 1. 8 Command-Line Arguments Introduction to Programming 1 1
  2. 2. ObjectivesAt 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 to your programs in Netbeans Introduction to Programming 1 2
  3. 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 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. Introduction to Programming 1 3
  4. 4. 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. Introduction to Programming 1 4
  5. 5. Command-line Arguments● In Java, when you invoke an application, the runtime system passes the command-line arguments to the applications 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. Introduction to Programming 1 5
  6. 6. Command-line Arguments● 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. 0 args 5 4 1 3 2 2 3 1 4 Introduction to Programming 1 6
  7. 7. Command-line Arguments● 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 } Introduction to Programming 1 7
  8. 8. 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.● Heres 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] isnt valid (not a number). Introduction to Programming 1 8
  9. 9. 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 } Introduction to Programming 1 9
  10. 10. Command-line Arguments in Netbeans● Now, how can we pass command-line arguments in Netbeans?● Assuming you already have a project and you have compiled it successfully, follow the steps to pass command- line arguments in Netbeans Introduction to Programming 1 10
  11. 11. Command-line Arguments in Netbeans● Click on Projects (encircled below). Introduction to Programming 1 11
  12. 12. Command-line Arguments in Netbeans● Right-click on the CommandLineExample icon, and a popup menu will appear. Click on Properties. Introduction to Programming 1 12
  13. 13. Command-line Arguments in Netbeans● The Project Properties dialog will then appear. Introduction to Programming 1 13
  14. 14. Command-line Arguments in Netbeans● Now, click on Run-> Running Project Introduction to Programming 1 14
  15. 15. Command-line Arguments in Netbeans● On the Arguments textbox, type the arguments you want to pass to your program. In this case we typed in the arguments 5 4 3 2 1. Then, click on the OK button. Introduction to Programming 1 15
  16. 16. Command-line Arguments in Netbeans● Now try to RUN your program. Introduction to Programming 1 16
  17. 17. 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 Netbeans Introduction to Programming 1 17
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×