Click here to Skip to main content
15,897,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need your help 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.
For example
semantic.txt
#1
10:tom
11:cat
12:bird
13:john
14:brown
15:cream
16:mat
17:mammal
18:animal
19:fur

#2
0:like:6
1:has:5
2:is_a:4
3:caught:3
4:owned_by:2
5:is_color:1
6:sat_on:0

#3
10:4:11
10:3:12
10:2:13
10:1:14
11:6:15
11:0:16
11:4:17
12:4:18
17:4:18
17:5:19


then my program that reads the txt file

C#
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();
            }
        }
    }

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
10:4:11
10:3:12
10:2:13
10:1:14

please help me change my code. thanx

[edit]Code block added and others sorted out - OriginalGriff[/edit]
Posted
Updated 21-Dec-12 12:29pm
v3
Comments
CHill60 21-Dec-12 7:02am    
You haven't actually said what your problem is. Also this might be urgent to you but it's not urgent to us. Use the Improve Question widget to remove "urgently" and to add in what the actual problem is. Then we may be able to help.
OriginalGriff 21-Dec-12 7:02am    
We can't - we have no idea what changes you need.
Does it work a bit? If so what bit? If not what doesn't work?
What happens? What should happen?
Basically: what help do you actually need?

1 solution

hi thank and am sorry for using urgently, am also sorry if you were not able to understand my question. the bit of the program works well for the part of reading the text file.
C#
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();

What i am asking is i need to add part of code which takes in user input in any of the following formats and gives otput that corresponds within #3:
for example if user enters
?:?:?- it will display the whole #3
10:?:? - it will display all lines in #3 that start with 10
10:4:11 - it will give output as true
thats my problem. hope i was able to make you understand. thank you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900