0

I have 3 different notepad files, Jack.txt Ken.txt and Wizard.txt how does this work in coding when I want to input in program for example, I input Ken and the program should load Ken.txt file, Jack for Jack.txt and so on.

The coding below is very basic and unfinished at the moment but no matter what I enter it loads the "Jack.txt" file. Would this work if I separate the coding into loop so when I enter "Wizard" it will loop till they find Wizard.txt file, if not an error message should appear.

    //Prompt for input
    System.Console.WriteLine("Please enter the name");
    System.Console.Write("Name> ");
        string name = System.Console.ReadLine();

    // Fetch the file from input
    string text = System.IO.File.ReadAllText(@"D:\Users\Jack\Documents\Test\Jack.txt");
    string text1 = System.IO.File.ReadAllText(@"D:\Users\Jack\Documents\Test\Ken.txt");
    string text2 = System.IO.File.ReadAllText(@"D:\Users\Jack\Documents\Test\Wizard.txt");

    // Display the attributes to the console.
    System.Console.WriteLine(" ");
    System.Console.WriteLine("{0}", text);


  }
 }
}
Philip Kendall
  • 4,304
  • 1
  • 23
  • 42
Jack Dalton
  • 13
  • 1
  • 2
  • Your code is pretty much there, but it will be nay on impossible to read without the line breaks – Sayse Aug 18 '13 at 21:11
  • 1
    You could add a switch statement that checks what the user has entered and then loads the correct file depending on what the user typed in matches "Jack", "Ken" or "Wizard"... see http://msdn.microsoft.com/en-us/library/06tc147t(v=vs.90).aspx – dotnethaggis Aug 18 '13 at 21:11

4 Answers4

1

It loads all files not only Jacks, but since you have hardcoded the text variable into the output and that refers to the Jack-file it's the only file you see.

However, if you want to choose between those three according to the name the user entered, so this works as desired:

string name = System.Console.ReadLine();
string textContent = "";
string dir = @"D:\Users\Jack\Documents\Test"; 
if(name.Equals("Jack", StringComparison.OrdinalIgnoreCase))
{
    textContent = File.ReadAllText(Path.Combine(dir, "Jack.txt"));
}
else if(name.Equals("Ken", StringComparison.OrdinalIgnoreCase))
{
    textContent = File.ReadAllText(Path.Combine(dir, "Ken.txt"));            
}
else if(name.Equals("Jack", StringComparison.OrdinalIgnoreCase))
{
    textContent = File.ReadAllText(Path.Combine(dir, "Wizard.txt"));
}
else
{
     // output error or ask for another name
}
System.Console.WriteLine(" ");
System.Console.WriteLine("{0}", textContent);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 1
    It would be cleaner in a switch statement in my opinion. – dotnethaggis Aug 18 '13 at 21:13
  • I have tried your coding Tim, however the textContent = File.ReadAllText(Path.Combine(dir, "Jack.txt")); coding displays an error "File" and "Path" does not exist in the current context. This implies the same for Ken.txt and Wizard.txt – Jack Dalton Aug 18 '13 at 21:27
  • 1
    @DotNetHaggis: A switch doesn't support case insensitive string comparison(for example) if that is desired (see my answer). – Tim Schmelter Aug 18 '13 at 21:28
  • @JackDalton: Eihter specify the full path to the `System.IO.File` and `System.IO.Path` classes or add the `using System.IO` to the top of the file. – Tim Schmelter Aug 18 '13 at 21:31
  • 1
    @TimSchmelter The marked answer seems to be using a switch. Just seems cleaner but I agree with the case insensitive string. You could just use ToLower(). – dotnethaggis Aug 18 '13 at 21:44
  • @DotNetHaggis: `ToLower` is not the correct way to check case-insensitively. It's inefficient and open for culture specific issues. You won't pass the turkey test for instance. See J.Skeets answer here http://stackoverflow.com/questions/234591/upper-vs-lower-case which links it http://www.moserware.com/2008/02/does-your-code-pass-turkey-test.html – Tim Schmelter Aug 18 '13 at 21:46
  • 1
    @TimSchmelter thank you. I've learnt why to use and not use certain methods. I've voted up your answer due to this justification. – dotnethaggis Aug 18 '13 at 21:50
1

Something like this:

//Prompt for input
System.Console.WriteLine("Please enter the name");
System.Console.Write("Name> ");

string name = System.Console.ReadLine();
string text;
if (new[] {"Jack", "Ken", "Wizard"}.Contains(name))
{
    // Fetch the file from input
    text = System.IO.File.ReadAllText(@"D:\Users\Jack\Documents\Test\" + name + ".txt");
}

// Display the attributes to the console.
System.Console.WriteLine("");
System.Console.WriteLine("{0}", text);
Alex Siepman
  • 2,499
  • 23
  • 31
0

You want to use an 'if' statement to decide whether to print one or the other:

if (name == "Jack") {
    Console.WriteLine(text);
}
else if (name == "Ken") {
     ...
}
Rob G
  • 3,496
  • 1
  • 20
  • 29
  • Alternatively, use string.Format("c:\\your\\path\\here\\{0}", inputString) as the location argument and let them type whatever name they want.. – Rob G Aug 18 '13 at 21:33
0

You have to use a conditional statement (if-else, switch, etc.)

See my code below for an example. You may edit it as you wish.

        //Prompt for input
        System.Console.WriteLine( "Please enter the name" );
        System.Console.Write( "Name> " );
        string name = System.Console.ReadLine();

        /*
         *  Notice how I don't load the files here?
         *  If one of the files is 100 MB, your program will use 100 MB of memory and possibly more.
        */
        string text;

        //Display the attributes to the console.
        System.Console.WriteLine( " " );


        // Add a conditional switch statement.
        switch ( name ) // This is similar to using if-else statements.
        {
            case "Jack":
                text = System.IO.File.ReadAllText( @"D:\Users\Jack\Documents\Test\Jack.txt" );
                Console.WriteLine( text );

                break; // This is used to leave the switch statement.

            case "Ken":
                text = System.IO.File.ReadAllText( @"D:\Users\Jack\Documents\Test\Ken.txt" );
                Console.WriteLine( text );

                break;

            case "Wizard":
                text = System.IO.File.ReadAllText( @"D:\Users\Jack\Documents\Test\Wizard.txt" );
                Console.WriteLine( text );

                break;

            default: // This is when the program can't match any values above.
                Console.WriteLine( "Error! No-one exists with that name!" );

                break;
        }
Kayla
  • 485
  • 5
  • 14
  • 1
    Thanks, I have tried this and it's working now. Although I have to edit the coding a little as I may have to add more .txt files! – Jack Dalton Aug 18 '13 at 21:43