I have the following kotlin function that takes in a string "value", then attempts to parse "value" as both a LocalDateTime and an Int. It returns a Pair of the parsed value of "value" and an enum that specifies which of the three types it was determined to be.
enum class RangeValueType{
Int,
Date,
String
}
fun parseValue(value: String): Pair<RangeValueType, Any> {
val rangeDate: LocalDateTime
val rangeInt: Int
//try date first
try {
//ISO_LOCAL_DATE_TIME e.g. 2011-12-03T10:15:30 for Dec. 3rd, 2011 & 10:15 30 sec.
//can't use basic date b/c e.g. 20111203 is indistinguishable from an int.
rangeDate = LocalDateTime.parse(value)
//it parsed to a date, so return the date
return Pair(RangeValueType.Date, rangeDate)
}catch (dtpe: DateTimeParseException) {
//not a date
}
//try number next
try{
rangeInt = value.toInt()
//it parsed to an int, so return the int
return Pair(RangeValueType.Int, rangeInt)
}catch (nfe: NumberFormatException)
{
//not an int
}
//not date or int, must be a string
return Pair(RangeValueType.String, value)
}
My principal concern is that I am using caught exceptions as a means of flow control, which I understand to generally be a Bad Idea. I know C# has a "TryParse" method for exactly this kind of case, but I wasn't able to find anything for Kotlin / Java.
Is there a better way to parse the values into their proper types?