2

How to convert the string input taken using Console.ReadLine() function in a C# code??? Suppose I have created 2 integer variables a and b. Now I want to take from user the values of a and b. How can this be performed in C#?

1

7 Answers 7

9

Another option, that I usually use is int.TryParse

int retunedInt;
bool conversionSucceed = int.TryParse("your string", out retunedInt);

so it's good fit for fault tollerant pattern like:

if(!int.TryParse("your string", out retunedInt)) 
  throw new FormatException("Not well formatted string");
Sign up to request clarification or add additional context in comments.

3 Comments

+1. Out of curiosity if you're throwing an exception anyway, why not use int.parse and handle the exception that may throw?
@keyboardP: 1. You can handle some presice (custom exception you raise) and continue running program 2. You may think to not use exceptino at all, and just handle a flow in some way.
Ah okay I see. Usually I use TryParse for your second reason but I see that a program may have custom exceptions and logging which would be useful. Just making sure I hadn't missed some secret TryParse usage :D
2

You can use it with Int32.TryParse();

Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded.

int i;
bool b = Int32.TryParse(yourstring, out i);

Comments

2

Try this (make sure they input valid string):

int a = int.Parse(Console.ReadLine());

Also this:

int a;
string input;
do
{
    input = Console.ReadLine();

} while (!int.TryParse(input, out a));

1 Comment

You can also use int.TryParse is you are not sure the input is a string and you want to avoid the exception.
1

You can use Convert.ToInt32():

Convert.ToInt32(input);

Or TryParse()

bool success = Int32.TryParse(value, out number);

Comments

1

You can use int.TryParse

int number;
bool result = Int32.TryParse(value, out number);

The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails. It eliminates the need to use exception handling to test for a FormatException in the event that s is invalid and cannot be successfully parsed. Reference

Comments

1

Use Int32.TryParse to avoid exceptions in case your user doesn't type an integer number

string userInput = Console.ReadLine();
int a;
if (Int32.TryParse(userInput, out a))
    Console.WriteLine("You have typed an integer number");
else
    Console.WriteLine("Your text is not an integer number");

Comments

0

Use int.TryParse like:

int a;
Console.WriteLine("Enter number: ");
while (!int.TryParse(Console.ReadLine(), out a))
{
    Console.Write("\nEnter valid number (integer): ");
}

Console.WriteLine("The number entered: {0}", a);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.