0

I've been coding a program that stores student data (name and age) in a .txt file. I am now doing the delete method. However, when the user enters a string, I want it to compare the input to the strings in my List<string>, that's full of names. Code:

    string tempFileName;
    string inputSel; // Selection string for delete
    Console.WriteLine(" -- Deleting Grade {0} -- ", grade);
    Console.WriteLine("- Enter a student name to delete: ");
    foreach (string file in fileNames)
    {
        tempFileName = file.Replace(".txt", "");
        studentNames.Add(tempFileName);
    }
    foreach (string name in studentNames)
    {
        Console.Write("{0}\n", name);
    }
    Console.WriteLine();
    Console.Write("> ");
    inputSel = Console.ReadLine();
    string input = inputSel.ToLower();
    string tempString;
    bool foundString = false;
    foreach (string file in studentNames)
    {
        tempString = file.ToLower();
        if (inputSel == tempString)
        {
            foundString = true;
        }
    }
    if (!foundString)
    {
        Console.WriteLine("Wrong name entered!");
        Console.WriteLine("Returning to grades menu..");
        Console.WriteLine("Press any key to continue.");
        Console.ReadKey();
        return;
    }  

As you can see, the program stores inputSel into input (ToLower()), then compares each string in studentNames List<string>, and if it finds a match it flips the foundString bool, but even if I enter a name that matches (for example, it says JacobMusterson, I enter JacobMusterson, it should skip the "student not found", but it doesn't.

0

5 Answers 5

2

you should use input not inputSel

if (input == tempString)
{
    foundString = true;
}

because of line:

string input = inputSel.ToLower();

where you are assinging to input lower version of inputSel

I suggest you to use IngonreCase in string.Compare for not making ToLower()

var b =  string.Compare("a","A",StringComparison.OrdinalIgnoreCase);

it will return 0 if equal see here

EDIT:

personally I would use:

var exists = studentNames.Any(x=>string.Compare(x,inputSel,StringComparison.OrdinalIgnoreCase)==0);
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, I can't believe I missed that. Thanks a lot! (Why can't I accept answers?)
@Ilan321 you can, but you have to wait several minutes, popup says that
1

you can simply do it this way:

Boolean foundString = studentNames.Exists(name => name.ToLower().Equals(input));

Comments

1

Why not simply use,

if(list.Contains(s)){
    //found
}else{
    //not found
}

Where list is List<String> and s is a String

Comments

0

It will be more readable and effective if you will use Contains method of List:

foreach (string file in studentNames)
    {
        tempString = file.ToLower();
        if (inputSel == tempString)
        {
            foundString = true;
        }
    }
if (!foundString)
    {
        Console.WriteLine("Wrong name entered!");
        Console.WriteLine("Returning to grades menu..");
        Console.WriteLine("Press any key to continue.");
        Console.ReadKey();
        return;
    }  

can be rewrite:

if(!studentNames.Contains(inputSel, StringComparer.Create(CultureInfo.InvariantCulture, true)))
{
            Console.WriteLine("Wrong name entered!");
            Console.WriteLine("Returning to grades menu..");
            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
            return;
}

Comments

0

Just a small comment on your foreach-loop. You always iterating through all entries in the loop also if you already found out that the string is in the collection.

You can improve the performance of your code by replacing the last foreach.

Try:

bool foundString = studentNames.Select(file => file.ToLower()).Any(tempString => inputSel == tempString);

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.