-3
public class QuoteGenerator
{
    public static randomQuote()
    {
        string t = "Quotes.txt";
        List<string> Quotes = new List<string>();
        using (StreamReader quoteReader = new StreamReader(t))
        {
            string line = "";
            while ((line = quoteReader.ReadLine()) != null)
            {
                Quotes.Add(line);
            }
        }
        string[] response = Quotes.ToArray();
        string[] shuffle = Classes.RandomStringArrayTool.RandomizeStrings(response);

        return (shuffle[0]);
    }
}

Here's what's working and I thought my StreamReader code above would work the same:

    public string randomQuote()
    {
        string[] response = new string[] {"The fortune you seek, is in another bot"
            , "Someone has Googled you recently"
            , "This fortune no good. Try another"
            , "404 fortune not found"};
        string[] shuffle = Classes.RandomStringArrayTool.RandomizeStrings(response);
        return shuffle[0];
    }

I need to return the first line of quote from the StreamReader Method, how come the code I put together doesn't seem to work? I've thought about hard-coding the quotes but maybe it's a good idea to save them in a text file. I guess I don't understand how using StreamReader work. Can anyone please explain, I've only been coding since July. Thank you!

kcd
  • 37
  • 1
  • 1
  • 5
  • http://stackoverflow.com/questions/1262965/how-do-i-read-a-specified-line-in-a-text-file – Illedan Nov 13 '16 at 17:12
  • _"how come the code I put together doesn't seem to work?"_ -- doesn't work **how**? What _specifically_ is the problem you are having? Error messages? Incorrect output? Exception thrown? Fix your question so that you include a good [mcve], and a clear, detailed explanation of what precisely is the problem and what you've done so far to try to solve it. Be sure you include the _exact_ text of any error or exception message, and complete stack traces for exceptions. – Peter Duniho Nov 13 '16 at 20:39
  • Sorry for lack of details in my question, I just started coding, things are a little difficult for me to describe. There were no error messages and the code compiled, it's just there were no output. Ray Wilson was able to point out what I needed to double check. – kcd Nov 14 '16 at 01:26

1 Answers1

0

Assuming your Quotes.txt file is in the bin directory the StreamReader code works fine. The only thing obvious is that you are not specifying a return type for the randomQuote method.

public static string randomQuote()

Ray Wilson
  • 16
  • 1
  • It was both the string type and the location of the file that needed to be fixed. I think I just need a new pair of glasses. But as far as the file location, I thought just by adding a text item to the Project would be enough and I wouldn't need to use the UNC path of the file. It turned out that's all it needed. I also need to learn how to debug properly. Thank you for the assist. – kcd Nov 14 '16 at 01:31