Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading an integer from user input

Tags:

c#

input

What I am looking for is how to read an integer that was given by the user from the command line (console project). I primarily know C++ and have started down the C# path. I know that Console.ReadLine(); only takes a char/string. So in short I am looking for the integer version of this.

Just to give you an idea of what I'm doing exactly:

Console.WriteLine("1. Add account."); Console.WriteLine("Enter choice: "); Console.ReadLine(); // Needs to take in int rather than string or char. 

I have been looking for quite a while for this. I have found a lot on C but not C#. I did find however a thread, on another site, that suggested to convert from char to int. I'm sure there has to be a more direct way than converting.

like image 760
TomG Avatar asked Jun 27 '14 04:06

TomG


People also ask

How can you take integer data from the user?

Use Python built-in input() function to take integer input from the user. This input() function returns string data and it can be stored in a string variable. Then use the int() function to parse into an integer value.

How do you read an integer?

ReadLine() method in C# reads a string value from the console. If we want to read an integer value from the console, we first have to input the integer value in a string and then convert it to an integer. The int. Parse() method is then used to convert a string to an integer value in C#.


2 Answers

You can convert the string to integer using Convert.ToInt32() function

int intTemp = Convert.ToInt32(Console.ReadLine()); 
like image 85
CodingDefined Avatar answered Oct 13 '22 06:10

CodingDefined


I would suggest you use TryParse:

Console.WriteLine("1. Add account."); Console.WriteLine("Enter choice: "); string input = Console.ReadLine(); int number; Int32.TryParse(input, out number); 

This way, your application does not throw an exception, if you try to parse something like "1q" or "23e", because somebody made a faulty input.

Int32.TryParse returns a boolean value, so you can use it in an if statement, to see whether or not you need to branch of your code:

int number; if(!Int32.TryParse(input, out number)) {    //no, not able to parse, repeat, throw exception, use fallback value? } 

To your question: You will not find a solution to read an integer because ReadLine() reads the whole command line, threfor returns a string. What you can do is, try to convert this input into and int16/32/64 variable.

There are several methods for this:

  • Int.Parse()
  • Convert.ToInt()
  • Int.TryParse()

If you are in doubt about the input, which is to be converted, always go for the TryParse methods, no matter if you try to parse strings, int variable or what not.

Update In C# 7.0 out variables can be declared directly where they are passed in as an argument, so the above code could be condensed into this:

if(Int32.TryParse(input, out int number)) {    /* Yes input could be parsed and we can now use number in this code block        scope */ } else  {    /* No, input could not be parsed to an integer */ } 

A complete example would look like this:

class Program {     static void Main(string[] args)     {         Console.WriteLine("Hello World!");         var foo = Console.ReadLine();         if (int.TryParse(foo, out int number1)) {             Console.WriteLine($"{number1} is a number");         }         else         {             Console.WriteLine($"{foo} is not a number");         }         Console.WriteLine($"The value of the variable {nameof(number1)} is {number1}");         Console.ReadLine();     } } 

Here you can see, that the variable number1 does get initialized even if the input is not a number and has the value 0 regardless, so it is valid even outside the declaring if block

like image 31
Marco Avatar answered Oct 13 '22 05:10

Marco