ArgumentOutOfRange异常C#

时间:2012-10-20 21:24:28

标签: c# streamreader textreader

我正在完成我的作业,我必须做一个程序,将文件中的简单字母(如E和F)扩展到连续制作,也可以在文件夹中给出,例如E + T EF等。无论如何,代码显示下面给出了一个超出范围异常的参数。我在java中制作了相同的代码,一切正常。我不知道为什么在C#中它给了我这个例外。请给我一些建议!!

我忘了把我正在阅读的文件:

EFT

A +()

电子

E + T |È-T | T

T * F | T / F | F

一个|(E)

public void generare(){

        String N = null;
        String T = null;
        String S = null;
        String[] P = null;

        TextReader tr = new StreamReader("dateIntrare.txt");

        try
        {

            N = tr.ReadLine();
            T = tr.ReadLine();
            S = tr.ReadLine();
            P = new String[N.Length];

            for (int i = 0; i < N.Length; i++)
            {
                P[i] = tr.ReadLine();
            }

            tr.Close();

            Console.WriteLine("Neterminale: N = " + N);
            Console.WriteLine("Terminale:  T = " + T);
            Console.WriteLine("Productii ");

            for (int i = 0; i < P.Length; i++)
                Console.WriteLine("\t" + P[i]);

            Console.WriteLine("Start: S = " + S);

            Boolean gata = false;

            String iesire = S.Substring(0, S.Length);

            Console.WriteLine("\nRezultat");
            Console.Write("\t");

            while ((gata == false) && (iesire.Length < 50))
            {

                Console.Write(iesire);

                Boolean ok = false;

                for (int i = iesire.Length - 1; i >= 0 && ok == false; i--)
                {
                    for (int j = 0; j < N.Length && ok == false; j++)
                        if (N[j] == iesire[i])
                        {
                            String s1 = iesire.Substring(0, i);
                            String s2 = iesire.Substring(i + 1, iesire.Length); // HERE IS THE EXCEPTION TAKING PLACE

                            String inlocuire = P[N.IndexOf(iesire[i])];
                            String[] optiuni = null;

                            String[] st = inlocuire.Split('|');
                            int k = 0;

                            foreach (String now in st)
                            {
                                k++;
                            }

                            optiuni = new String[k];
                            st = inlocuire.Split('|');

                            k = 0;

                            foreach (string next in st)
                            {
                                optiuni[k++] = next;
                            }

                            Random rand = new Random();
                            int randNr = rand.Next(optiuni.Length);

                            String inlocuireRandom = optiuni[randNr];

                            iesire = s1 + inlocuireRandom + s2;

                            ok = true;

                        }
                }

                if (ok == false)
                {
                    gata = true;
                }

                else
                {

                    if (iesire.Length < 50)
                        Console.Write(" => ");
                }
            }
        }

        catch (FileNotFoundException)
        {
            Console.WriteLine("Eroare, fisierul nu exista!");
        }

        Console.WriteLine();
    }

2 个答案:

答案 0 :(得分:4)

  

但是为什么在java中工作而不在这里?我很困惑

如有疑问,请阅读the documentation。在Java中,substring的2参数重载采用起始索引和结束索引。在.NET中,第二个参数是要采用的字符数,不是结束索引。

所以你可能想要

String s2 = iesire.Substring(i + 1, iesire.Length - i - 1);

或者更简单一点,只需使用1参数版本,它将从指定索引开始获取所有字符:

String s2 = iesire.Substring(i + 1);

(我也在Java中使用它......)

从根本上说,值得退后一步,弄清楚为什么你不能为自己解决这个问题......即使你以前错过了它:

  • 查看在代码中抛出异常的行
  • 查看实际抛出异常的方法(在本例中为String.Substring
  • 仔细查看异常消息(这是一个非常好的提示!)以及任何嵌套的exceptins
  • 仔细阅读相关方法的文档,尤其是描述参数和例外的部分

答案 1 :(得分:2)

将代码从Java移植到c#时,这是一个常见的错误。

Java中的子串开始&amp;结束参数但在c#中它们是起始和长度

相关问题