benleggiero
09/10/2017, 6:56 PMval numberOfDoorsIndex = args.indexOf("--numberOfDoors")
val numberOfDoors: Int
if (numberOfDoorsIndex > 0 && args.size > numberOfDoorsIndex) {
val userSetNumberOfDoorsString = args[numberOfDoorsIndex]
try {
numberOfDoors = max(3, abs(userSetNumberOfDoorsString.toInt()))
}
catch (ex: NumberFormatException) {
numberOfDoors = 3 // error here
}
}
error:
Error:(22, 13) Kotlin: Val cannot be reassigned
Obviously it isn't going to be; if the NumberFormatException
is thrown, it was never assigned in the first place!kartikpatodi
09/10/2017, 7:02 PMrusshwolf
09/10/2017, 8:56 PMtry
block would happen before or after the exception is thrown. In any event, maybe try using if
and try
as expressions, ie
val numberOfDoors = if (numberOfDoorsIndex > 0 && args.size > numberOfDoorsIndex) {
val userSetNumberOfDoorsString = args[numberOfDoorsIndex]
try {
max(3, abs(userSetNumberOfDoorsString.toInt()))
}
catch (ex: NumberFormatException) {
3
}
}
kartikpatodi
09/11/2017, 12:29 AMrusshwolf
09/11/2017, 12:33 AMvar
kartikpatodi
09/11/2017, 12:47 AMmarstran
09/11/2017, 8:32 AMnumberOfDoors = try { ... } catch (ex: NumberFormatException) { ... }