Question

I am doing my homework and I have to do a program that extends simple letters from a file, like E and F, to continuous productions, given also in the folder, such as E+T E-F etc. Anyway the code shown below gives me an argument out of range exception. I crafted the same code in java and all works fine. I don't know why in C# it gives me this exception. Please give me some advice!!

I forgot to put the file that I'm reading from:

EFT

a+()

E

E+T|E-T|T

T*F|T/F|F

a|(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();
    }
Was it helpful?

Solution

But why in java works and here not? I'm confused

When in doubt, read the documentation. In Java, the 2-parameter overload of substring takes a start index and an end index. In .NET, the second parameter is the number of characters to take, not an end index.

So you probably want

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

Or to be simpler about it, just use the 1-parameter version, which takes all the characters from the specified index onwards:

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

(I'd use that in Java too...)

Fundamentally though, it's worth taking a step back and working out why you couldn't work this out for yourself... even if you missed it before:

  • Look at the line that threw the exception in your code
  • Look at which method actually threw the exception (String.Substring in this case)
  • Look at the exception message carefully (it's a really good hint!) and also any nested exceptins
  • Read the documentation for the relevant method carefully, especially the sections describing the parameters and exceptions

OTHER TIPS

This is a common mistake while porting codes from Java to c#.

Substring in Java takes start & end parameters but in c# they are start and length

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top