2

How to make this piece of code loop asking for input from the user until int.TryParse()

is successful?

//setX
    public void setX()
    {
        //take the input from the user
        string temp;
        int temp2;
        System.Console.WriteLine("Enter a value for X:");
        temp = System.Console.ReadLine();
        if (int.TryParse(temp, out temp2))
            x = temp2;
        else
            System.Console.WriteLine("You must enter an integer type value"); 'need to make it ask user for another input if first one was of invalid type'
    }

Version of the code after the helpful answer:

 //setX
    public void setX()
    {
        //take the input from the user
        string temp;
        int temp2;
        System.Console.WriteLine("Enter a value for X:");
        temp = System.Console.ReadLine();
        if (int.TryParse(temp, out temp2))
            x = temp2;
        else
        {
            Console.WriteLine("The value must be of integer type");
            while (!int.TryParse(Console.ReadLine(), out temp2))
                Console.WriteLine("The value must be of integer type");
            x = temp2;
        }
    }

4 Answers 4

10
while (!int.TryParse(Console.ReadLine(), out mynum))
    Console.WriteLine("Try again");

edit:

public void setX() {
    Console.Write("Enter a value for X (int): ");
    while (!int.TryParse(Console.ReadLine(), out x))
        Console.Write("The value must be of integer type, try again: ");
}

Try this. I personally prefer to use while, but do .. while is also valid solution. The thing is that I don't really want to print error message before any input. However while has also problem with more complicated input that can't be pushed into one line. It really depends on what exactly you need. In some cases I'd even recommend to use goto even tho some people would probably track me down and slap me with a fish because of it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I've edited my original post with the updated code. Is there a way to make this code shorter yet it would do the same thing? Just curious.
I've edited a little bit shorter solution of whole setX method for you
4

Even though the question has been already marked as answered, do-while loops are much better for validating user input.

Notice your code:

Console.WriteLine("The value must be of integer type");
while (!int.TryParse(Console.ReadLine(), out temp2))
    Console.WriteLine("The value must be of integer type");

You have the same code at top and bottom. This can be changed:

do {
    Console.WriteLine("The value must be of integer type");
} while (!int.TryParse(Console.ReadLine(), out temp2));

1 Comment

Thanks. That's what I've been looking for.
2

This can help too

public int fun()
{
    int Choice=0;
    try 
    {
        Choice = int.Parse(Console.ReadLine());
        return choice;
    } 
    catch (Exception) 
    {
        return fun(); 
    }
}

1 Comment

Don't use an exception for flow control in 2020 and beyond because int.TryParse() is available (and exceptions for flow control is an anti-pattern).
1

I've been wondering quite a lot, but I just figured it out!

    int number;
    bool check;
    do
    {
        Console.WriteLine("Enter an integer:");
        check = int.TryParse(Console.ReadLine(), out num1);
    }
    while (!check);

This code will loop until the user has entered an integer number. This way, the program doesn't simply report an error, but instead immediately allows the user to input again another, correct value.

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.