Passing multiple parameters from the command line in Java I am working on a project in which I need to pass multiple arguments from the command line-

Below is the use case I have-


From the command line, I will be passing atleast four paramaters- noOfThreads, noOfTasks, startRange and tableName1, 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.
Secondly, I can pass five parameters instead of four just like above- So five parameters can be- noOfThreads, noOfTasks, startRange , tableName1 and tableName2. So here tableName2 is extra. so if I am passing these five thing, then I need to store them in a variable and here I am passing tableName1 and tableName2 as two tables so I will be storing these two tables in a string list.
Thirdly, I can pass six parameters instead of five just like above- So sixparameters can be- noOfThreads, noOfTasks, startRange , tableName1, tableName2 and tableName3. So here tableName3 is extra. so if I am passing these six thing, then I need to store them in a variable and here I am passing tableName1 , tableName2 and tableName3 as three tables so I will be storing these three tables in a string list again.


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?

Below is my 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);
}
}
Extract out the common logic before the if. The following is more or less the same:

noOfThreads = Integer.parseInt(args[0]);
noOfTasks = Integer.parseInt(args[1]);
startRange = Integer.parseInt(args[2]);
tableName1 = args[3];
databaseNames.add(tableName1);
if (args.length &gt;= 5) {
    tableName2 = args[4];
    databaseNames.add(tableName2);
}
if (args.length &gt;= 6) {
    tableName3 = args[5];
    databaseNames.add(tableName3);
}

Note that running the program with less than five parameters only prints an exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at ...Main.main(Main.java:16)


You should handle this and print a user friendly error message or usage help.