title: Passing multiple parameters from the command line in Java
body: <p>I am working on a project in which I need to pass multiple arguments from the command line-</p>
  
  <p>Below is the use case I have-</p>
  
  <ol>
  <li><p>From the command line, I will be passing atleast four paramaters- <code>noOfThreads</code>, <code>noOfTasks</code>, <code>startRange</code> and <code>tableName1</code>, so if I am passing these four thing, then I need to store them in a variable and for any table names- I need to add it into the string list so that I can use them in my other code.</p></li>
  <li><p>Secondly, I can pass five parameters instead of four just like above- So five parameters can be- <code>noOfThreads</code>, <code>noOfTasks</code>, <code>startRange</code> , <code>tableName1</code> and <code>tableName2</code>. So here <code>tableName2</code> is extra. so if I am passing these five thing, then I need to store them in a variable and here I am passing <code>tableName1</code> and <code>tableName2</code> as two tables so I will be storing these two tables in a string list.</p></li>
  <li><p>Thirdly, I can pass six parameters instead of five just like above- So sixparameters can be- <code>noOfThreads</code>, <code>noOfTasks</code>, <code>startRange</code> , <code>tableName1</code>, <code>tableName2</code> and <code>tableName3</code>. So here <code>tableName3</code> is extra. so if I am passing these six thing, then I need to store them in a variable and here I am passing <code>tableName1</code> , <code>tableName2</code> and <code>tableName3</code> as three tables so I will be storing these three tables in a string list again.</p></li>
  </ol>
  
  <p>So for above scenario, I have the below code with me. It is looking very ugly currently as I have lot of repetition in my code as mentioned below. Is there any way I can make it more cleaner?</p>
  
  <p>Below is my code-</p>
  
  <pre><code>private static List&lt;String&gt; databaseNames = new ArrayList&lt;String&gt;();
  private static int noOfThreads;
  private static int noOfTasks;
  private static int startRange;
  private static String tableName1;
  private static String tableName2;
  private static String tableName3;
  
  public static void main(String[] args) {
  
  if (args.length &gt; 0 &amp;&amp; args.length &lt; 5) {
  
      noOfThreads = Integer.parseInt(args[0]);
      noOfTasks = Integer.parseInt(args[1]);
      startRange = Integer.parseInt(args[2]);
      tableName1 = args[3];
      databaseNames.add(tableName1);
  } else if (args.length &gt; 0 &amp;&amp; args.length &lt; 6) {
      noOfThreads = Integer.parseInt(args[0]);
      noOfTasks = Integer.parseInt(args[1]);
      startRange = Integer.parseInt(args[2]);
      tableName1 = args[3];
      tableName2 = args[4];
      databaseNames.add(tableName1);
      databaseNames.add(tableName2);
  } else {
      noOfThreads = Integer.parseInt(args[0]);
      noOfTasks = Integer.parseInt(args[1]);
      startRange = Integer.parseInt(args[2]);
      tableName1 = args[3];
      tableName2 = args[4];
      tableName3 = args[5];
      databaseNames.add(tableName1);
      databaseNames.add(tableName2);
      databaseNames.add(tableName3);
  }
  }
  </code></pre>