发生异常索引超出了数组的范围

时间:2018-11-03 01:34:42

标签: c# arrays ascii

运行代码时出现以下错误:

发生异常索引超出了数组的范围。

我的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ASCII

{
    class CharacterFrequency
    {
        private char ch;
        private int frequency;

        public char Ch
        {
            get { return ch; }
            set { ch = value; }
        }
        public int Frequency
        {
            get { return frequency; }
            set { frequency = value; }
        }
    }

    class Program
    {
        public string InputFileName = "wap.txt";
        public string OutputFileName = "Output.txt";
        public string FilePath = "";

        public static SortedDictionary<char, ulong> Count(string stringToCount)
        {
            SortedDictionary<char, ulong> characterCount = new SortedDictionary<char, ulong>();


            foreach (var character in stringToCount)
            {
                if (!characterCount.ContainsKey(character)) // added character to dictionary if only character is absent in charactercount
                {
                    characterCount.Add(character, 1);
                }
                else
                {
                    characterCount[character]++; // increemetned count
                }
            }
            return characterCount;
        }

        static void Main(string[] args)
        {
            Program p = new Program();
            CharacterFrequency obj = new CharacterFrequency();
            StreamWriter streamWriter = new StreamWriter(p.OutputFileName);

            try
            {
                p.InputFileName = args[0]; // get input
                p.OutputFileName = args[1]; // get output
                p.FilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + p.InputFileName; // get file path
                if (File.Exists(p.FilePath)) // checked if file exist
                {
                    string data = File.ReadAllText(p.FilePath); // read the data in string
                    var count = Program.Count(data); // pass the string to count method
                    foreach (var character in count)
                    {
                        streamWriter.WriteLine(character.Key + "(" + (int)character.Key + ")" + "\t" + character.Value); // write data to output file
                    }
                    streamWriter.Close();
                    Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("Please provide input File"); // if input file absent sent message to user to place input file
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occured " + ex.Message.ToString());
            }
        }
    }
}

该程序应该读取ASCII文本文件并计算每个字符出现在文本文件中的次数。它应该输出带有此信息的文本文件,并且字符频率对象应存储在数组中。任何帮助,将不胜感激。

2 个答案:

答案 0 :(得分:0)

我看到的唯一可能引发该异常的行是:

p.InputFileName = args[0]; // get input
p.OutputFileName = args[1]; // get output

args数组具有命令行中的参数。因此,运行此命令时,是否要在命令行中传递参数?喜欢:

program.exe input.txt output.txt

但是您要为它们声明变量:

public string InputFileName = "wap.txt";
public string OutputFileName = "Output.txt";

那么也许这就是您想要的?

p.InputFileName = InputFileName;
p.OutputFileName = OutputFileName;

答案 1 :(得分:0)

public static SortedDictionary<char, ulong> Count(string stringToCount)
{
   var characterCount = new SortedDictionary<char, ulong>();

   foreach (var character in stringToCount)
      if (!characterCount.ContainsKey(character))       
         characterCount.Add(character, 1);
      else
         characterCount[character]++; //

   return characterCount;
}

static void Main(string[] args)
{

   var filePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);  
   var inputFileName = Path.Combine(filePath, args[0]);
   var outputFileName = Path.Combine(filePath, args[2]);

   // read data, count chars
   var count = Count(File.ReadAllText(inputFileName)); 

   // create output
   var outPut = count.Select(x => $"{x.Key}\t{x.Value}");

   // write it
   File.WriteAllLines(outputFileName, outPut);

}

所以

  • 如果需要组合路径,请使用Path.Combine

  • 如果您需要投影或转换东西,请使用Select

  • 如果您需要写一些行File.WriteAllLines

显然,这缺少错误检查以及传递的内容。