Solved

command line argument

Posted on 2004-08-20
6
245 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 82
java. non-English characters encoding problem. intellij idea 3 74
Java array passed to SQL where clause 23 62
Basic Java Case or If-Else statement... 3 43
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

910 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

16 Experts available now in Live!

Get 1:1 Help Now