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 7512963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:02:02+00:00 2026-05-30T00:02:02+00:00

I am currently implementing a simulation in Java that requires an input of about

  • 0

I am currently implementing a simulation in Java that requires an input of about 30 different parameters. Eventually, I want to be able to read these parameters from a file and also from a GUI, but I am just focusing on the file input for now. My simulation requires parameters that are of different types: Strings, ints and doubles and I currently have them as fields for the simulation e.g.

private String simName;
private int initialPopulationSize;
private double replacementRate;

Because these parameters are not all the same type I can’t store them in an array and I have to read each one in separately using the same kind of code about 30 times. An example for three parameters:

//scanner set up and reading each line, looking for "(key)=(param)" regex matches
//if statement to check each param name against the key matched in file. Store param in that field if the name matches.
String key = m.group(1);
if (key.equals(PKEY_SIM_NAME)) {
    if (simNameSet) {
        throw new IllegalStateException("multiple values for simulation name");
    }
    this.simName = m.group(2);
    simNameSet = true;

} else if (key.equals(PKEY_INITIAL_SIZE)) {
    if (initialSizeSet) {
        throw new IllegalStateException("multiple values for initial population size");
    }
    this.initialPopulationSize = Integer.parseInt(m.group(2));
    initialPopulationSize = true;

 } else if (key.equals(PKEY_MUT_REPLACEMENT)) {
    if (replacementRateSet) {
        throw new IllegalStateException("multiple values for replacement rate");
    }
    this.replacementRate = Double.parseDouble(m.group(2));
    replacementRateSet = true;
 }
    //Add nauseum for each parameter.....

So I currently have a long and unweildly method for reading in parameters, and I will likely need to do the same again for reading from gui.

The best alternative I have thought of is to read everything into String fields first. That way I can write a simple few lines for reading in using a Map. Something like this (untested code):

//This time with a paramMap<String, String>, scanner set up as before
if (!paramMap.containsKey(key)) {
    paramMap.put(key, m.group(2));
}
else{
    throw new IllegalStateException("multiple values for initial population size");
}

However, this will be inconvenient when it comes to using these parameters from the Map, since I will have to cast the non-String params whenever and wherever I want to use them.

At this point I feel like this is my best approach. I want to know if anyone a little more experienced can come up with a better strategy for dealing with this kind of situation before I move on.

  • 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-05-30T00:02:05+00:00Added an answer on May 30, 2026 at 12:02 am

    You can define a base Parameter class or interface like:

    interface Parameter {
        void parse(String s);
        Object getValue();
        ...
    }
    

    and a class for each type of parameter you want to have, e.g. IntParameter, DoubleParameter, StringParameter. Here’s a sketch of an IntParameter:

    class IntParameter implements Parameter {
        private int value;
    
        public void parse(String s) {
            value = Integer.parseInt(s);
        }
    
        public Object getValue() {
            return value;
        }
        ...
    }
    

    Then you can store your parameters in a Map<String, Parameter> and populate from various sources, like command-line options or properties.


    A more type-safe but convoluted solution can be achieved if you don’t store the value in parameter objects, but make parameters static objects that are used to access their values.
    This is illustrated on the following example:

    abstract class Parameter {
        private String name;
    
        public Parameter(String name) {
            this.name = name;
        }
    
        public abstract Object parse(String s);
    }
    
    class IntParameter extends Parameter {
        public static final IntParameter ANSWER = new IntParameter("answer");
        // Add more options here.
    
        public IntParameter(String name) { super(name); }
    
        public Object parse(String s) {
            return Integer.parseInt(s);
        }
    }
    
    class Parameters {
        private Map<Parameter, Object> params = new HashMap<Parameter, Object>();
    
        public int get(IntParameter p) {
            return (Integer)params.get(p);
        }
    
        public void put(IntParameter p, int value) {
            params.put(p, value);
        }
    
        public void putString(Parameter p, String value) {
            params.put(p, p.parse(value));
        }
    }
    

    This allows you to access parameter in a type safe manner:

    Parameters params = new Parameters();
    params.putString(IntParameter.ANSWER, "42"); // parse and store the value
    int value = p.get(IntParameter.ANSWER);
    

    The solution can be extended to other types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently implementing a JavaScript library that keeps track of the history of changes
I'm currently implementing a .NET wrapper for a Java library by using JNI to
i am currently implementing a binary tree in c++ and i want to traverse
I'm currently implementing a HTML canvas based webapp that features panning. Is there a
I'm currently implementing a client application that POST's a file over HTTP and have
I'm currently implementing a histogram that will show a very large scale data using
I am currently implementing a form in CodeIgniter that needs to have a drop
I'm currently in the process of implementing a number of different assignment algorithms for
I am currently implementing a view in Android that involves using a larger than
I am currently implementing Quartz.net in a simple application that should execute a piece

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.