0

when i run the program MyKwic sampleText(text file saved in the project) the compiler cannot trace the sampleText.

using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;

public class Mykwic
{
    static void Main(string[] args)
    {
        if (args.Length == 0)
        {
            Console.WriteLine("Error: Missing file name");
            return;
        }

        StreamReader reader = null;
        Hashtable table = new Hashtable();

        try
        {
            reader = new StreamReader(args[0]);

            for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
            {
                string[] words = GetWords(line);

                foreach (string word in words)
                {
                    string iword = word.ToLower();
                    if (table.ContainsKey(iword))
                        table[iword] = (int)table[iword] + 1;
                    else
                        table[iword] = 1;
                }

            }
            SortedList list = new SortedList(table);

            Console.WriteLine("{0} unique words found in {1}", table.Count, args[0]);

            foreach (DictionaryEntry entry in list)
                Console.WriteLine("{0} ({1})", entry.Key, entry.Value);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            if (reader != null)
                reader.Close();
        }
    }

    static string[] GetWords(string line)
    {
        ArrayList al = new ArrayList();

        int i = 0;
        string word;
        char[] characters = line.ToCharArray();

        while ((word = GetNextWord(line, characters, ref i)) != null)
            al.Add(word);

        string[] words = new string[al.Count];
        al.CopyTo(words);
        return words;
    }

    static string GetNextWord(string line, char[] characters, ref int i)
    {
        while (i < characters.Length && !Char.IsLetterOrDigit(characters[i]))
            i++;
        if (i == characters.Length)
            return null;

        int start = i;

        while (i < characters.Length && Char.IsLetterOrDigit(characters[i]))
            i++;
        return line.Substring(start, i - start);
    }

}

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Ramy Mahrous
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.