2

I want to check if user's input is a number. If yes I want the function to keep running else want to alert him and run it again.

Console.WriteLine(String.Concat("choose your action" ,Environment.NewLine ,
                                "1.Deposit", Environment.NewLine,
                                "2.Withdraw", Environment.NewLine,
                                "3.CheckAccount"));
string c = Console.ReadLine();
int value = Convert.ToInt32(c);

if (value==char.IsLetterOrDigit(value)) //<----- no good why?
{
    switch (value)
    {
        case 1:
            Deposit();
            return;
        case 2:
            Withdraw();
            return;
        case 3:
            CheckAccount();
            return;
    }
}
1
  • 1
    No need for using String.Concat mate! There's an operator for that, namely the string concatenation operator +, which is optimized into a call to String.Concat. Commented Jan 13, 2013 at 15:43

3 Answers 3

17

Just use:

string c = Console.ReadLine();
int value;
if (int.TryParse(c, out value)) { /*Operate*/ }

EDIT: to adapt the code to the author's comment:

if (int.TryParse(c, out value) && value >= 1 && value <= 3) { /*Operate*/ }
Sign up to request clarification or add additional context in comments.

4 Comments

how do i add a check to see that the number is between 1-3 and its not a float or double?
@user1942298 int.TryParse will return false in case the number is not an integer. I've edited my post to adapt it to your second request.
@Mir: why you have used out here? Is it allowed to omit it ?
@Destructor out is the output that you are going to use if the parsing(converting string to int) is successful.
1

int value = Convert.ToInt32(c); this is going to fail if c is not a string consisting of integers only. use try catch to handle this situation.

1 Comment

I'd use TryParse, it's much faster. I haven't tested on .NET but Mono gave me 649 milliseconds for your method and 47 milliseconds for TryParse for this test: pastebin.com/HfLjqTqg.
0
    Write:
`string chooseNum = Console.ReadLine();
int val;\\in which the value will pass
if (!checkNum )
{
    Console.WriteLine("Its not a Number");
}*\\ We Will Directly get out of it if its not a Number*
else
{ 
if (val >= 1 && val <=3)
        switch (val)
    {
        case 1:
            Deposit();
            return;
        case 2:
            Withdraw();
            return;
        case 3:
            CheckAccount();
            return;
    }
}
    else
        Console.WriteLine("Number Must be b/w 1 and 3");
}
 `

1 Comment

Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?

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.