HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Passing multiple parameters from the command line in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thelinepassingjavamultiplecommandfromparameters

Problem

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 databaseNames = new ArrayList();
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 > 0 && args.length 0 && args.length < 6) {
noOfThreads = Integer.parseInt(args[0]);
noOfTasks = Integer.parseInt(args[1]);
startRange = Integer.parseInt(args[2]);
tableName1 = args[3

Solution

-
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 >= 5) {
    tableName2 = args[4];
    databaseNames.add(tableName2);
}
if (args.length >= 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.

Code Snippets

noOfThreads = Integer.parseInt(args[0]);
noOfTasks = Integer.parseInt(args[1]);
startRange = Integer.parseInt(args[2]);
tableName1 = args[3];
databaseNames.add(tableName1);
if (args.length >= 5) {
    tableName2 = args[4];
    databaseNames.add(tableName2);
}
if (args.length >= 6) {
    tableName3 = args[5];
    databaseNames.add(tableName3);
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at ...Main.main(Main.java:16)

Context

StackExchange Code Review Q#21531, answer score: 4

Revisions (0)

No revisions yet.