Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9261683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:11:32+00:00 2026-06-18T13:11:32+00:00

I am working on a project in which I need to pass multiple arguments

  • 0

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-

  1. 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.

  2. 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.

  3. 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<String> databaseNames = new ArrayList<String>();
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 < 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 > 0 && args.length < 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);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T13:11:33+00:00Added an answer on June 18, 2026 at 1:11 pm

    You have a lot of redundancy, and when you see that think refactoring using methods to simplify. That’s is in effect just what I’d do. Also, consider having String[] field for your tableNames, String[] tableNames which will allow you to declare it as an array of whatever size is needed once you know your parameter count. For example:

    public void fillParams(String[] args) {
        if (args.length < someMinimum) {
           // throw some exception
        }
        noOfThreads = Integer.parseInt(args[0]);
        noOfTasks = Integer.parseInt(args[1]);
        startRange = Integer.parseInt(args[2]);
    
        tableNames = new String[args.length - 3];
        for (int i = 0; i < tableNames.length; i++) {
           tableNames[i] = args[i + 3];
           databaseNames.add(tableNames[i]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help. I am working on a project where I have multiple pages
I am working on a project in which I need to download and parse
I am working on a project in which I need to generate 8 random
I'm working on a project in which I need to process a huge amount
I am working on a project in which I need to change the time
Currently we are working on a project in which we need to come up
Currently I am working with the project in which I need to parse complex
I am working on a homework project in which we need to generate a
I'm working on using a project which uses jQuery, and I need to dynamically
I am working on a Windows C project which is string-intensive: I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.