I am currently implementing a simulation in Java that requires input of about 30 different parameters. In the end, I want to be able to read these parameters from a file, as well as from the graphical interface, but now I'm just focused on entering files. My modeling requires parameters that have different types: strings, ints and double, and currently I have them as fields for modeling, for example.
private String simName; private int initialPopulationSize; private double replacementRate;
Since these parameters are not all the same, I cannot store them in an array, and I have to read each separately using the same 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 obscure method for reading in parameters, and I probably have to do the same thing again for reading from gui.
The best alternative that I thought of is to read everything in string fields first. That way, I can write some simple lines to read using 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 discard parameters other than String whenever and wherever I want to use them.
At this moment, I feel that this is my best approach. I want to know if someone more experienced can develop a better strategy to deal with this situation before I start.