reset password
Author Message
dliang
Posts: 35
Posted 23:15 May 09, 2019 |

 

Is this correct? 

java MyProgram -Help
public static void main(String [] args){
}
if (args.length > 0 && args[0].equals("-help"){
printHelp();
System.exit(0);
}

wcwir
Posts: 75
Posted 23:34 May 09, 2019 |
dliang wrote:

 

Is this correct? 

java MyProgram -Help
public static void main(String [] args){
}
if (args.length > 0 && args[0].equals("-help"){
printHelp();
System.exit(0);
}

Not quite:

These three lines are correct:

if (args.length > 0 && args[0].equals("-help"){
    printHelp();
    System.exit(0);
}

They need to go inside your main method. (And of course you need to implement the printHelp() method.)

The class itself you write as usual:

public class MyProgram {

    // main and other methods go here.

}

And then when you run your program from the command line, you can run it either like you always do:

java MyProgram

or like this:

java MyProgram -help

For more on command-line arguments see section 7.13 of the textbook.

Last edited by wcwir at 23:35 May 09, 2019.