Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8714341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:34:29+00:00 2026-06-13T05:34:29+00:00

I am doing my homework and I have to do a program that extends

  • 0

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();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:34:30+00:00Added an answer on June 13, 2026 at 5:34 am

    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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So for Homework I have to create this program that reads from a text
I'd like to create a simple program that will block certain websites for the
I'm doing a homework assignment in C. I have to build a calculator that
I am doing homework but I have problem with non-RestFul routes. My spec is:
I'm doing some homework and while I have some experience with SML, Haskell has
I am doing homework in Matlab, calculating numeric integration using different methods like simpson,
I am doing this homework where I am supposed to read a .txt file
I'm doing a homework project that requires this: Below you will find the code
Me and my friend having a problem while doing a homework assignment. We have
So I am doing homework and I am stuck on one spot. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.