Solved

command line argument

Posted on 2004-08-20
6
243 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
Comment Utility
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
Comment Utility
Ehm... on second thought  || should be &&
0
 
LVL 2

Accepted Solution

by:
beermequik earned 20 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 35

Expert Comment

by:girionis
Comment Utility
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
Comment Utility
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
Comment Utility
The convention is:

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now