Java Command line arguments

2019-01-03 07:11发布

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

9条回答
放荡不羁爱自由
2楼-- · 2019-01-03 07:31

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.

查看更多
聊天终结者
3楼-- · 2019-01-03 07:32

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
    }
}
查看更多
女痞
4楼-- · 2019-01-03 07:32

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).

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-01-03 07:32

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

public static void main(String str[]) {

    boolean b = str[0].equals("a");

    System.out.println(b);

}

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

查看更多
爷的心禁止访问
6楼-- · 2019-01-03 07:34

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)
查看更多
【Aperson】
7楼-- · 2019-01-03 07:35

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
查看更多
登录 后发表回答