© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
34 replies
Steadhaven

Do while with int.TryParse - how?

I am trying to create a C-sharp program, that ask the user to pick a number between 1 and 3. It will then use that number to print one of three names I have in an array. This is how far I got:
string[] names = new string[] { "Peter", "John", "Paul" };

Console.Write("Select a number from 1-3: ");
string numberText = Console.ReadLine();

bool isValidNum = false;
int.TryParse(numberText, out int num)

do () {
    
} while (isValidNum == false);
string[] names = new string[] { "Peter", "John", "Paul" };

Console.Write("Select a number from 1-3: ");
string numberText = Console.ReadLine();

bool isValidNum = false;
int.TryParse(numberText, out int num)

do () {
    
} while (isValidNum == false);

I wanted the do-while to continue until the nubmerText is an actual number I can parse to Int, but also then be a valid number from 1-3 but I am kind of confused as to how to proceed.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How do I typecast a string with a int
C#CC# / help
2y ago
how do i get digits of an int?
C#CC# / help
3y ago