25

I am trying to detect whether the 'a' was entered as the first string argument.

1

9 Answers 9

42

Use the apache commons cli if you plan on extending that past a single arg.

"The Apache Commons CLI library provides an API for parsing command line options passed to programs. It's also able to print help messages detailing the options available for a command line tool."

Commons CLI supports different types of options:

  • POSIX like options (ie. tar -zxvf foo.tar.gz)
  • GNU like long options (ie. du --human-readable --max-depth=1)
  • Java like properties (ie. java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo)
  • Short options with value attached (ie. gcc -O2 foo.c)
  • long options with single hyphen (ie. ant -projecthelp)
Sign up to request clarification or add additional context in comments.

Comments

39
public class YourClass {
    public static void main(String[] args) {
        if (args.length > 0 && args[0].equals("a")){
            //...
        }
    }
}

Comments

20

Every Java program starts with

public static void main(String[] args) {

That array of type String that main() takes as a parameter holds the command line arguments to your program. If the user runs your program as

$ java myProgram a

then args[0] will hold the String "a".

Comments

5

Command-line arguments are passed in the first String[] parameter to main(), e.g.

public static void main( String[] args ) {
}

In the example above, args contains all the command-line arguments.

The short, sweet answer to the question posed is:

public static void main( String[] args ) {
    if( args.length > 0 && args[0].equals( "a" ) ) {
        // first argument is "a"
    } else {
        // oh noes!?
    }
}

Comments

2

Command line arguments are accessible via String[] args parameter of main method.

For first argument you can check args[0]

entire code would look like

public static void main(String[] args) {
    if ("a".equals(args[0])) {
         // do something
    }
}

Comments

1

Your main method has a String[] argument. That contain the arguments that have been passed to your applications (it's often called args, but that's not a requirement).

Comments

1

Try to pass value a and compare using the equals method like this:

public static void main(String str[]) {
    
    if (str.length > 0) {  
        boolean b = str[0].equals("a");
        System.out.println(b);
    }
}

Follow this link to know more about Command line argument in Java

1 Comment

You should always check the length of an array before accessing entries.
0

As everyone else has said... the .equals method is what you need.

In the off chance you used something like:

if(argv[0] == "a")

then it does not work because == compares the location of the two objects (physical equality) rather than the contents (logical equality).

Since "a" from the command line and "a" in the source for your program are allocated in two different places the == cannot be used. You have to use the equals method which will check to see that both strings have the same characters.

Another note... "a" == "a" will work in many cases, because Strings are special in Java, but 99.99999999999999% of the time you want to use .equals.

Comments

0

Command line arguments are stored as strings in the String array String[] args that is passed tomain()`.

java [program name] [arg1,arg2 ,..]

Command line arguments are the inputs that accept from the command prompt while running the program. The arguments passed can be anything. Which is stored in the args[] array.

//Display all command line information
         class ArgDemo{
            public static void main(String args[]){
            System.out.println("there are "+args.length+"command-line arguments.");
            for(int i=0;i<args.length;i++)
            System.out.println("args["+i+"]:"+args[i]);
    }
    }

Example:

java Argdemo one two

The output will be:

there are 2 command line arguments:
they are:
arg[0]:one
arg[1]:two

1 Comment

In your example, what does the first line javastack.java. mean? I don't know for sure what the It in It's used to accept input from the command prompt while running the program. is referring to, but the sequence is 1) parse command 2) setup parameters 3) run program. I don't know for sure what the This in This essentially speeds up the program execution when the program depends on user input. is referring to, but there is nothing in parameter passing to speed up the program execution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.