Reputation: 58030
I am trying to detect whether the 'a' was entered as the first string argument.
Upvotes: 25
Views: 137367
Reputation: 69
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
Upvotes: 1
Reputation: 71
Command line arguments are stored as strings in the String
array String[] args that is passed to
main()`.
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
Upvotes: 0
Reputation: 405675
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".
Upvotes: 20
Reputation: 29381
public class YourClass {
public static void main(String[] args) {
if (args.length > 0 && args[0].equals("a")){
//...
}
}
}
Upvotes: 39
Reputation: 14521
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:
Upvotes: 42
Reputation: 61516
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.
Upvotes: 0
Reputation: 87210
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
}
}
Upvotes: 2
Reputation: 48369
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!?
}
}
Upvotes: 5
Reputation: 308001
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).
Upvotes: 1