-3

I am trying to create an ai. I have got it to write a topic with information to a text file but cant get it to read. this is what i have so far. Any ideas on why it isnt working?

string information = "blank";
Console.WriteLine("Hello my name is Lou-C.");
Console.WriteLine("What is yours?");
string name = Console.ReadLine();
Console.WriteLine("Hello {0}, What do you want to talk about?", name);
string topic = Console.ReadLine();
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\simon.light\Documents\Visual Studio 2013\Projects\Lou-c\Lou-c\information.txt");
if (lines.Contains("beat"))
{
    Console.WriteLine("Aaahh, {0}, I have heard of this before");
    Console.WriteLine("Tell me what you know about this");
    information = Console.ReadLine();
}
else
{
Console.WriteLine("{0}? I don't think i have come across this before.", topic);
Console.WriteLine("Tell me about it");
information = Console.ReadLine();
}

using (System.IO.StreamWriter file = 
new System.IO.StreamWriter(@"C:\Users\simon.light\Documents\Visual Studio 2013\Projects\Lou-c\Lou-c\information.txt", true))
{
    file.WriteLine("${0} - %{1} - by {2}", topic, information, name);
} 
Rob
  • 26,989
  • 16
  • 82
  • 98
  • 6
    Can you expand on "isn't working"? – James Thorpe Sep 22 '15 at 12:43
  • Based on his question and code I think he is referencing this line as causing him trouble: ` string[] lines = System.IO.File.ReadAllLines(@"C:\Users\simon.light\Documents\Visual Studio 2013\Projects\Lou-c\Lou-c\information.txt");` . OP can you verify? – IfTrue Sep 22 '15 at 12:46
  • Why do you use `File.ReadAllLines` for reading but `StreamWriter.WriteLine` for writing? Why not `File.AppendLine`? – Corak Sep 22 '15 at 12:46
  • 2
    This has nothing to do with AI, so I'm going to update the title and remove the tag. – Rob Sep 22 '15 at 12:48
  • Additionally, the logic seems wrong. `topic` is read in from the command line, but then you check if `"beat"` exists in the file, rather than if `topic` does. – Rob Sep 22 '15 at 12:51
  • That is not how you check contents of a string array. http://stackoverflow.com/questions/2912476/using-c-sharp-to-check-if-string-contains-a-string-in-string-array – DotNetRussell Sep 22 '15 at 12:52
  • Btw. are you aware that `lines.Contains("beat")` will only be true if the file contains at least one line with `"beat"` *and nothing else*. Or did you mean to look if any of the lines contains `"beat"` instead? – Corak Sep 22 '15 at 12:52
  • `lines.any((line)=>line.contains("beat"));` – theB Sep 22 '15 at 12:55
  • isnt working:every time i input data that is already in the txt file is dosent complete the if statement but the else statement – Simon Light Sep 22 '15 at 13:29

1 Answers1

0

To check for any line that contains "beat"

if (lines.Any(s => s.Contains("beat")))

However, if you used ReadAllText then your contains would work!

string text = System.IO.File.ReadAllText(...);
if (text.Contains("beat"))
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73