i need your help urgently finishing my program below. The program is for showing how the semantic network works. the program reads from the text file then when user inputs a question it outputs answer related to the network. i have attached the txt file( semantic.txt)
if the user enters a code in any of the following format the program will read #3 and output corresponding data
/*
* ?:?:?
* 1:?:?
* ?:1:?
* ?:?:1
* 1:1:?
* 1:?:1
* ?:1:1
* 1:1:1 true / false
*/
i.e if 10:?:?
output
<pre lang="css">10:4:11
10:3:12
10:2:13
10:1:14
</pre>
please help me change my code. thanks

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using ConsoleApplication1;

namespace trial1
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader streamReader = new StreamReader("D:\\semantic.txt");

            int block = 0;



            while (!streamReader.EndOfStream)
            {
                string line = streamReader.ReadLine().Trim();
                if (line.CompareTo("") == 0)
                {
                    continue;
                }
                if (line.CompareTo("#1") == 0)
                {
                    Console.WriteLine("We have #1");
                    block = 1;
                    continue;

                }
                if (line.CompareTo("#2") == 0)
                {

                    Console.WriteLine("\n\nWe have #2");
                    block = 2;
                    continue;
                }
                if (line.CompareTo("#3") == 0)
                {
                    Console.WriteLine("\n\nWe have #3");
                    block = 3;
                    continue;
                }
                if (block == 1)
                {
                    string[] first = line.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (first.Length == 2)
                    {
                        int code = Convert.ToInt32(first[0].Trim());
                        string name = first[1].Trim();
                        Console.WriteLine("code = {0}, name = '{1}'", code, name);
                    }
                }

                if (block == 2)
                {
                    string[] second = line.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (second.Length == 3)
                    {
                        int code = Convert.ToInt32(second[0].Trim());
                        string connector = second[1].Trim();
                        int code2 = Convert.ToInt32(second[2].Trim());
                        Console.WriteLine("code = {0}, connector = '{1}', code2 = {2}", code, connector, code2);

                    }
                }
                if (block == 3)
                {
                    string[] third = line.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (third.Length == 3)
                    {
                        int name = Convert.ToInt32(third[0].Trim());
                        int code2 = Convert.ToInt32(third[1].Trim());
                        int name1 = Convert.ToInt32(third[2].Trim());
                        Console.WriteLine("name = {0}, code2 = {1}, name1 = {2}", name, code2, name1);

                    }
                }
                }
            streamReader.Close();

            StreamReader Reader = new StreamReader("D:\\semantic.txt");
            string text = Reader.ReadToEnd();



            // THE THIRD BLOCK 

            int x = text.IndexOf("#3");
            string z = text.Substring(x + 2);
            int k = Convert.ToInt32("23");


            //VERIFY
            Console.WriteLine("\n\nEnter the code for verification.");
            string str = Console.ReadLine();

            string [] str1 = str.Split(' ');
            foreach (string input in str1)

            {

                if (z.Contains(input))
                {
                    Console.WriteLine(z.Contains(input));
                    int th = z.IndexOf(input);
                    int thr = z.LastIndexOf(input);
                    Console.WriteLine(z.Substring(th, thr-th));
                }

                else
                {
                    Console.WriteLine(z.Contains(input));
                }
            }

            Console.ReadLine();
            }

        }

    }

Recommended Answers

All 4 Replies

What exactly is it doing wrong?

Hi . Thanks for looking at my problem.Its not doing anything wrong its just that i cant figure out the part of code to give output for when the user enters the specified input. I tried String.Contains but am not able to output the only strings that contain the specifieed substring. I hope you understand me.

Sorry to take so long replying. I think I've got what you want. I kind of had to rewrite the last part. Getting the logic figured out was bear, but I think you'll be happy:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
    namespace trial1
    {
        class Program
        {
            static void Main(string[] args)
            {
                StreamReader streamReader = new StreamReader("C:\\semantic1.txt");
            int block = 0;
            while (!streamReader.EndOfStream)
            {
                string line = streamReader.ReadLine().Trim();
                if (line.CompareTo("") == 0)
                {
                    continue;
                }
                if (line.CompareTo("#1") == 0)
                {
                    Console.WriteLine("We have #1");
                    block = 1;
                    continue;
                }
                if (line.CompareTo("#2") == 0)
                {
                    Console.WriteLine("\n\nWe have #2");
                    block = 2;
                    continue;
                }
                if (line.CompareTo("#3") == 0)
                {
                    Console.WriteLine("\n\nWe have #3");
                    block = 3;
                    continue;
                }
                if (block == 1)
                {
                    string[] first = line.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (first.Length == 2)
                    {
                        int code = Convert.ToInt32(first[0].Trim());
                        string name = first[1].Trim();
                        Console.WriteLine("code = {0}, name = '{1}'", code, name);
                    }
                }
                if (block == 2)
                {
                    string[] second = line.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (second.Length == 3)
                    {
                        int code = Convert.ToInt32(second[0].Trim());
                        string connector = second[1].Trim();
                        int code2 = Convert.ToInt32(second[2].Trim());
                        Console.WriteLine("code = {0}, connector = '{1}', code2 = {2}", code, connector, code2);
                    }
                }
                if (block == 3)
                {
                    string[] third = line.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (third.Length == 3)
                    {
                        int name = Convert.ToInt32(third[0].Trim());
                        int code2 = Convert.ToInt32(third[1].Trim());
                        int name1 = Convert.ToInt32(third[2].Trim());
                        Console.WriteLine("name = {0}, code2 = {1}, name1 = {2}", name, code2, name1);
                    }
                }
            }
            streamReader.Close();
            string quit = "";
            while (!(quit.StartsWith("q")))


            {
                Console.WriteLine("\n\nEnter the code for verification.\n'q' to quit");
                string[] str = Console.ReadLine().Split(":".ToCharArray());
                if (str[0].StartsWith("q"))
                    break;
                //Put whole file into a list
                List<string> thrd = File.ReadAllLines(@"C:\semantic1.txt")
                    .ToList<string>();
                //Separate out the #3 section
                thrd = thrd.Skip<string>(thrd.IndexOf("#3") + 1).ToList<string>();
                //Use input to filter pattern.
                if (str[0] != "?")
                    thrd = FilterList(thrd, str[0], 0);
                else if (str[1] != "?")
                    thrd = FilterList(thrd, str[1], 1);
                else if (str[2] != "?")
                    thrd = FilterList(thrd, str[2], 2);
                else
                    thrd.Clear();
                //Print out results
                foreach (string line in thrd)
                    Console.WriteLine(line);
            }



            }
            public static List<string> FilterList(List<string> list, string ToFilter, int Indx)
            {
                list = list

                     .Where(line => !string.IsNullOrEmpty(line))

                     .Select(line => line.Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))

                     .Where(values => values[Indx].Equals(ToFilter, StringComparison.CurrentCultureIgnoreCase))

                     .Select(values => string.Join(":", values))

                     .ToList<string>();                
                return list;
            }
        }
    }

I added a while loop for testing, it was a pain to keep restarting the program lol. As you've probably noticed I had to get into LINQ to generate the logic. I treated each line as 3 separate values. Basically it generates the list of possible answers up to 3 times. Filtering out all the items that the relevant value doesn't match the pattern, but not applying any filter to that value if a '?' was used.

Tinstaafl THANK YOU SO MUCH. Thank you again. I am really happy and thank you for sparing your time to help me. I also thank you for looking at my problem. I am really happy. May you be blessed and know that am grateful. Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.