0

How can check the input if it was "int or string " in console app ?

int x; x =int.parse(Console.ReadLine()); //I wanna check if the user put int or String in Console ..

10th Dec 2016, 10:26 PM
Abdulwahab Herbli
Abdulwahab Herbli - avatar
7 odpowiedzi
+ 3
string inputAge = Console.ReadLine(); try{ int age = Int32.Parse(inputAge); } catch(Exception e){ Console.WriteLine("Insert a Numeric Age"); }
10th Dec 2016, 10:53 PM
Nahuel
Nahuel - avatar
+ 2
You will always get a String from a input. You can convert that in a Int , float, or wathever later, but as init, it's a String.
10th Dec 2016, 10:35 PM
Nahuel
Nahuel - avatar
+ 2
I will recommend you using try-catch blocks, when trying to parse the string to int, so it doesn't break your app. There is a lesson in c# course here.
10th Dec 2016, 10:47 PM
Nahuel
Nahuel - avatar
0
I meant that user can input for e.g: int age; input string here so it will break the app so I wanna use if to go to start if it wasn't number ..
10th Dec 2016, 10:37 PM
Abdulwahab Herbli
Abdulwahab Herbli - avatar
0
can u give me an example please ?
10th Dec 2016, 10:50 PM
Abdulwahab Herbli
Abdulwahab Herbli - avatar
0
thanks ! :) I appreciate it
10th Dec 2016, 10:54 PM
Abdulwahab Herbli
Abdulwahab Herbli - avatar
0
x.getType();
11th Dec 2016, 4:38 PM
Dimitiko