How to assure that the user 's input is a correct type of value? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to assure that the user 's input is a correct type of value?

for example,when i need input of others' ages,i need to assure that one enter a integer. i am wondering whether there is another way to do the same thing,distinguishing wrong values, as exception do. Thanks.

20th Oct 2017, 6:47 PM
YuHai
YuHai - avatar
3 Answers
+ 1
If you truly want to process your own inputs, you can code something like my getInput function in this code. https://code.sololearn.com/cMMmkTfOxOoR/?ref=app
20th Oct 2017, 7:31 PM
John Wells
John Wells - avatar
+ 1
Well you can define a method similarly to int.TryParse in c#. something like Boolean TryParse(string input){ try { Integer.parseInt(input); return true; } catch{ return false; } } and then if(!TryParse(x)){ //some logic } I know that you have to use try catch, anyway it looks more elegant
20th Oct 2017, 7:06 PM
Tomasz
Tomasz - avatar
+ 1
thanks,sir.sorry for thanking you so late
22nd Oct 2017, 6:14 AM
YuHai
YuHai - avatar