Ask coding questions

← Back to all posts
how to take user input in the form of an integer
AaronPhilip

Please help me. This is so irritating like i just want to take two integers and add them. Instead, it takes it in the form of a string and just concatenates them.
:(

Thank you.

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
ebrahimpascal
SophiaJones7
AaronPhilip
Comments
hotnewtop
Coder100

Hello, to do this, you will have to use the Int class:

Note the ??. This is because if the user input was a, you wouldn't be able to convert it to a number and the variable would become null iirc.

AaronPhilip

@Coder100 Thank you :) it helped a lot

Coder100

no problem!! :D @AaronPhilip

PattanAhmed

@AaronPhilip Hi,
It's easy...
You just have to convert it into Int data type
Here is your code with explanation:-

That's it
Thanks!

Hope this helps

AaronPhilip

@PattanAhmed
Thank you :)

PattanAhmed

@AaronPhilip Welcome!
Happy to help you.

Please mark my answer if that helps
Thanks!

GoodCoderBadBoy

I've never used swift before, but looking things up, you may need to convert the readline() output into an int. I may be totally and utterly wrong though.

Coder100

not quite, you forgot to handle the cases when the input isn't an integer @GoodCoderBadBoy

GoodCoderBadBoy

@Coder100 thank you