What is the best practice for reading a large number of custom settings from a text file?

Posted by jawilmont on Programmers See other posts from Programmers or by jawilmont
Published on 2011-08-14T19:48:56Z Indexed on 2014/06/04 21:41 UTC
Read the original article Hit count: 162

Filed under:
|

So I have been looking through some code I wrote a few years ago for an economic simulation program. Each simulation has a large number of settings that can be saved to a file and later loaded back into the program to re-run the same/similar simulation. Some of the settings are optional or depend on what is being simulated.

The code to read back the parameters is basically one very large switch statement (with a few nested switch statements). I was wondering if there is a better way to handle this situation.

One line of the settings file might look like this:

#RA:1,MT:DiscriminatoryPriceKDoubleAuction,OF:Demo Output.csv,QM:100,NT:5000,KP:0.5 //continues...

And some of the code that would read that line:

switch( Character.toUpperCase( s.charAt(0) ) )
{
    case 'R':
         randSeed = Integer.valueOf( s.substring(3).trim() );
         break;
    case 'M':
          marketType = s.substring(3).trim();
          System.err.println("MarketType: " + marketType);
          break;
    case 'O':
          outputFileName = s.substring(3).trim() ;
          break;
    case 'Q':
          quantityOfMarkets = Integer.valueOf( s.substring(3).trim() );
          break;
    case 'N':
          maxTradesPerRound = Integer.valueOf( s.substring(3).trim() );
          break;
    case 'K':
          kParameter = Float.valueOf( s.substring(3).trim() );
          break;
 // continues...
}

© Programmers or respective owner

Related posts about java

Related posts about file-handling