Question

I have a section of code where I am trying to make it so the user puts in their name and I want it to recognise if it's an actually name. So to keep things simple I just want it to accept letters only.

I type a valid name in and it carries on to the next section of code. However when I put in invalid characters I get the "Invalid Name" message but after that no matter what I type in it just keeps saying "Invalid Name".

Console.WriteLine("Please Enter First Name");
bool isNotName = true;
string firstName = Console.ReadLine();

while (isNotName)
{
    if (Regex.IsMatch(firstName, @"^[a-zA-Z]+$"))
    {
        {
            Console.WriteLine("Welcome {0}", firstName);
        }
        isNotName = false;
    }     
    else
    {
        Console.WriteLine("Invalid Name");
        Console.ReadLine();
    }
 }
Was it helpful?

Solution

Console.WriteLine("Please Enter First Name");
bool isNotName = true;
string firstName = Console.ReadLine();

while (isNotName)
{
    if (Regex.IsMatch(firstName, @"^[a-zA-Z]+$"))
    {            
        Console.WriteLine("Welcome {0}", firstName);            
        isNotName = false;
    }    
    else
    {
        Console.WriteLine("Invalid Name");
        firstName = Console.ReadLine(); // <---- re-assign name here
    }
}

Also I would refactor your code to remove boolean flag:

Console.WriteLine("Please Enter First Name");   
string firstName = Console.ReadLine();

while(!Regex.IsMatch(firstName, @"^[a-zA-Z]+$"))
{
    Console.WriteLine("Invalid Name");
    firstName = Console.ReadLine();
}

Console.WriteLine("Welcome {0}", firstName);

OTHER TIPS

The firstName value is not updated in the loop, so try this :

string firstName = Console.ReadLine();

 while (isNotName)
{
    if (Regex.IsMatch(firstName, @"^[a-zA-Z]+$"))
    {
        {
        Console.WriteLine("Welcome {0}", firstName);
        }
        isNotName = false;
    }

    else
    {
        Console.WriteLine("Invalid Name");
        firstName = Console.ReadLine();
    }
 }

Your issue is here in the else part of the conditional:

Console.ReadLine();

It should be:

firstName = Console.ReadLine();

What's happening is that you're saving the user input the first time you read input from the console but not saving it in subsequent calls to ReadLine()

i.e. your code should be:

Console.WriteLine("Please Enter First Name");
bool isNotName = true;
string firstName = Console.ReadLine();

while (isNotName)
{
    if (Regex.IsMatch(firstName, @"^[a-zA-Z]+$"))
    {
        Console.WriteLine("Welcome {0}", firstName);
        isNotName = false;
    }     
    else
    {
        Console.WriteLine("Invalid Name");
        firstName = Console.ReadLine();
    }
}

You read the name once.

Use string firstName = Console.ReadLine(); at the begining of the while loop.

Try this in your else part

else
{
firstName = Console.ReadLine(); 
}

You are not setting the firstName variable in the subsequent readline:

   Console.WriteLine("Invalid Name");
   firstName = Console.ReadLine();

Just to try to be original, your peers would probably enjoy reading something like this:

string firstName;
while (!(Regex.IsMatch(firstName = Console.ReadLine(), @"^[a-zA-Z]+$")))
{
    Console.WriteLine("Invalid Name"); 
}

Console.WriteLine("Welcome {0}", firstName);

It's just for lulz though, assignment inside a conditional expression is always a bad idea. :)

The bug is that the value of firstName is never updated.

The code can be improved:

Console.WriteLine("Please Enter First Name");
string firstName = Console.ReadLine();
While (!Regex.IsMatch(firstName, @"^[a-zA-Z'-]+$")) {
    Console.WriteLine("Invalid Name");
    firstName = Console.ReadLine();
}
Console.WriteLine("Welcome {0}", firstName);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top