Solved

command line argument

Posted on 2004-08-20
6
248 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:manish14
6 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11850935
Does this help?

if (args.length != 5 || args.length != 4 )
{
  System.out.println();
  System.out.println("Usage: <arg0> <arg1> <arg2> <arg3> <arg4 optional>");
  return;
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 11850956
Ehm... on second thought  || should be &&
0
 
LVL 2

Accepted Solution

by:
beermequik earned 20 total points
ID: 11850978
In addition to above....
                   
          String baseFolder = args[0];
          String host = args[1];
          String username = args[2];
          String password = args[3];
          String optional = null;   // or set it to some default value you want it to be if its not passed
          // if the 5th param is there, read it!
          if (args.length == 5) {
              optional = args[4];
          }

         

         
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 35

Expert Comment

by:girionis
ID: 11850991
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;
}
0
 

Author Comment

by:manish14
ID: 11851253
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];
            }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11851835
The convention is:

System.out.println("Usage: <arg0> <arg1> <arg2> <arg3> [arg4]");
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question