I have a data file containing a combination of doubles and strings like this:
Rmax=2.3
Rmin=1.0
etc.
I’m using split() to cut out the = like this:
while ((strLine = br.readLine()) != null) {
String phrase = strLine;
try {
//splits to variable name, value
String[] parsed = phrase.split("[=]");
int parsed[0].toString() = parsed[1];
} catch (PatternSyntaxException e) {
System.out.println("Error: " + e.getMessage());
}
}
Now i’m trying to get the values initialized so that parsed[0] becomes the name of the integer and parsed[1] becomes the value. equivalent to the initializer
int name = value;
obviously the line
int parsed[0].toString() = parsed[1];
doesn’t work. how do i go about doing this?
How about this
As has been mentioned you have to catch NumberFormatException and handle a missing ‘=’ depending on whether these are possible.
If you know all possible fields you could use an enum for the field name.
Instead of using a Map you could use an Object which you set via reflection.