I am trying to detect whether the 'a' was entered as the first string argument.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
As everyone else has said... the .equals method is what you need.
In the off chance you used something like:
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.
Command line arguments are accessible via
String[] args
parameter ofmain
method.For first argument you can check
args[0]
entire code would look like
Your
main
method has aString[]
argument. That contain the arguments that have been passed to your applications (it's often calledargs
, but that's not a requirement).Try to pass value a and compare using the equals method like this:
Follow this link to know more about Command line argument in Java
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:
Command line arguments are stored as strings in the
String
arrayString[] args that is passed to
main()`.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.Example:
The output will be: