I'm wondering if there is any nice way how to read single attributes from formatted string in Groovy or even in Java.
I have a string containing some attributes separated by space. For example "2.1 20 Something true". The order is fixed and the "attribute type" is known (for the example first is Float, second is Integer, etc.). I need something similar to String.format() but other way round.
I know that I can split the string manually and read the values, but this makes the code too complicated like this:
String[] parsedText = "2.1 20 Something true".split(delimiter)
try {
firstVal = new Float(parsedText[0])
}
catch (NumberFormatException e) {
throw new RuntimeException("Bad data [0th position in data string], cannot read[{$parsedData[0]}], cannot convert to float")
}
...
Is there a better way? I'm pretty sure that at least in Groovy is:-)
Thanks!