The current behavior is actually correct and infact is not a bug. Let me explain the behavior.
When you read an input as any choice = io:readln("Enter choice 1 - 5: ");
the choice
variable is of type any
and it would contain a string
value. However, the way any
to typeX
(in this case int
) works is that, it will check whether the actual value inside the any-typed variable is of typeX
(int), and if so, then do the conversion.
In this case, the actual value inside the any-typed variable choice
is string
. Now if we try to convert this to int
it will fail, for the reason that it does not contain an integer inside. So the correct aproach is to first get the string value inside the any-type variable, and then convert the string to int in a second step. See the below example:
import ballerina/io;
function main(string... args) {
any choice = io:readln("Enter choice 1 - 5: ");
string choiceStr = <string>choice;
int choiceInt = check <int> choiceStr;
io:println(choiceInt);
}
But of course, reading the cmd input directly to a string like: string choice = io:readln("Enter choice 1 - 5: ");
is the better solution.