C# programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asif08
    New Member
    • Feb 2008
    • 4

    C# programming

    I am getting an error when I try to run the MyKwic . I entered MyKwic practiceFile(wh ich i saved in the project). However, the compiler does not seem to trace the practiceFile. Here is MyKwic:
    Code:
    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);
        }
    
    }
    Last edited by Plater; Feb 12 '08, 03:12 PM. Reason: Added [CODE] tags
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    What do you mean by "trace"?

    Comment

    Working...