Solved

command line argument

Posted on 2004-08-20
6
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 96
Eclipse with various Java releases 7 90
Print Rhino Java Array in Javascript 1 58
JDBC performance 8 35
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

751 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