manish14
asked on
command line argument
Hello
I have a basic question, in my application i have 4 arguments to pass before running the application no i want to add new 5th argument but it should be optional. So how i can do this
i have something like this
public static void main(String args[])
{
if (args.length != 4 )
{
System.out.println();
System.out.println("Usage: <arg0> <arg1> <arg2> <arg3> ");
return;
}
String baseFolder = args[0];
String host = args[1];
String username = args[2];
String password = args[3];
Now i want to add 5th optional argument, means on comand line if i pass 4 paramenter then also it should work (when 5th is not provided), if 5th is provided then it should consider 5th argument
how this can be done
System.out.println("Usage: <arg0> <arg1> <arg2> <arg3><arg4> ");
String baseFolder = args[0];
String host = args[1];
String username = args[2];
String password = args[3];
String optional = args[4];
Thanks
I have a basic question, in my application i have 4 arguments to pass before running the application no i want to add new 5th argument but it should be optional. So how i can do this
i have something like this
public static void main(String args[])
{
if (args.length != 4 )
{
System.out.println();
System.out.println("Usage:
return;
}
String baseFolder = args[0];
String host = args[1];
String username = args[2];
String password = args[3];
Now i want to add 5th optional argument, means on comand line if i pass 4 paramenter then also it should work (when 5th is not provided), if 5th is provided then it should consider 5th argument
how this can be done
System.out.println("Usage:
String baseFolder = args[0];
String host = args[1];
String username = args[2];
String password = args[3];
String optional = args[4];
Thanks
Ehm... on second thought || should be &&
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could also do:
if (args.length <= 5 && args.length >= 4 )
{
// stuff here
}
else
{
System.out.println();
System.out.println("Usage: <arg0> <arg1> <arg2> <arg3> <arg4 optional>");
return;
}
if (args.length <= 5 && args.length >= 4 )
{
// stuff here
}
else
{
System.out.println();
System.out.println("Usage:
return;
}
ASKER
complete solution is like this, if i say String optional = null then it gives null pointer exp if i try to access optional
if (args.length != 5 && args.length != 4 )
{
System.out.println();
System.out.println("Usage: <arg0> <arg1> <arg2> <arg3> <arg4 optional>");
return;
}
String baseFolder = args[0];
String host = args[1];
String username = args[2];
String password = args[3];
String optional = " ";
// if the 5th param is there, read it!
if (args.length == 5)
{
optional = args[4];
}
if (args.length != 5 && args.length != 4 )
{
System.out.println();
System.out.println("Usage:
return;
}
String baseFolder = args[0];
String host = args[1];
String username = args[2];
String password = args[3];
String optional = " ";
// if the 5th param is there, read it!
if (args.length == 5)
{
optional = args[4];
}
The convention is:
System.out.println("Usage: <arg0> <arg1> <arg2> <arg3> [arg4]");
System.out.println("Usage:
if (args.length != 5 || args.length != 4 )
{
System.out.println();
System.out.println("Usage:
return;
}