Solved

command line argument

Posted on 2004-08-20
6
252 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 69
Tomcat: Unable to run tomcat service. 2 24
throw exception 21 58
Need help aligning JRadioButton that put in a box using GridBagLayout 2 20
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…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

821 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