Forums

How to ADD inputs in a program

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

How to ADD inputs in a program

Postby alisterescabarte » Sat Feb 07, 2015 9:36 pm

class Add

def main
print 'Enter two numbers:'
x = Console.readLine
y = Console.readLine
print 'sum is', int.parse(x) + int.parse(y)
print 'done.'


that is the code i used when compiling in xamarin studio cobra language'

but i wonder why i get this error when compiling === Error: Argument 1 of method "parse" expects a non-nilable type (String), but the call is supplying a nilable type (String?). The declaration is "parse(s as String)", the call is supplying an arglist of type "(String?)". (printinput)
alisterescabarte
 
Posts: 1

Re: How to ADD inputs in a program

Postby Charles » Sun Feb 08, 2015 9:42 am

.readLine returns "String?" but .parse wants "String", in other words, nilable vs. non. You can change the code to:
x = Console.readLine ? ''

... in other words nil coalesce with an empty string. This will guarantee non-nil.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 31 guests