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

  • Home
  • SEARCH
  • 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 6682189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:40:37+00:00 2026-05-26T04:40:37+00:00

My code can detect { bracket error, if in file there is only }

  • 0

My code can detect { bracket error, if in file there is only } bracket then it will throw empty stack exception. I want to know how can I detect both bracket errors, if { this or } this bracket missing then it will show error.
class StackCS
{

    #region Vaiables
    Stack stack;
    char[] charArray;
    string[] stringArray;
    int linenumber;

    string chError = string.Empty;
    public string ChError { get { return chError +" "+ linenumber; } set { chError = value; } }

    public StackCS()
    {
        charArray = new char[1000];
        stack = new Stack();
    }

    #endregion 

    #region File

    private void ReadCharFromFile(string add)
    {
        using (StreamReader rdr = new StreamReader(add))
        {
            for (int i = 0; i < charArray.Length; i++)
            {
                charArray[i] = (char)rdr.Read();
            }
        }
    }

    public void ReadFile(string add)
    {
        ReadCharFromFile(add);
        stringArray = File.ReadAllLines(add);
    }

    #endregion

    #region Stack

    public void FillingStack()
    {
        for (int i = 0; i < charArray.Length; i++)
        {
            if (charArray[i] == '{')
            {
                stack.Push('{');
            }
            else if (charArray[i] == '}')
                stack.Pop();
        }
    }

    public void CheckingStack()
    {
        for (int i = 0; i < stringArray.Length; i++)
        {
            if (stack.Count != 0)
            {
                chError = "Error At:";
                linenumber = i;
            }
            else
            {
                chError = "No Error";
            }
        }
    }

    public override string ToString()
    {
        string temp = string.Empty;
        for (int i = 0; i < stringArray.Length; i++)
        {
            temp +=i+"\t\t"+stringArray[i] + "\n";
        }
        return temp;
    }

    #endregion

}

        static void Main(string[] args)
    {
        StackCS obj = new StackCS();
        try
        {
            // set the file location, according to ur PC setting

            obj.ReadFile(@"C:\Users\5609\Desktop\Class1.cs");
            obj.FillingStack();
            obj.CheckingStack();
            Console.WriteLine("Line Number" + "     " + "File Content"+"\n");
            Console.WriteLine(obj);
            Console.WriteLine(obj.ChError);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);

            obj.CheckingStack();
            Console.WriteLine("Line Number" + "     " + "File Content" + "\n");
            Console.WriteLine(obj);
            Console.WriteLine(obj.ChError);
        }
    }
  • 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-05-26T04:40:38+00:00Added an answer on May 26, 2026 at 4:40 am

    If you don’t have a final closing brace then something will be left on the stack, at the end simply return if you have any elements in the stack. If you dont the expression is invalid.

    The other case is that you have a closing brace without a formal expression in which case you encounter } but the stack is empty (error).

    So once you’ve checked the stack you have made sure that all expressions contained in { } are proper than that no elements were left unchecked on the stack so stack.Empty() is true then the expression is valid.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a code snippet that can detect the current user's page and then
I was wondering how I can detect code plagiarism with Javascript. I want to
Is there anyway this code can be refactored? The only difference is the order
Is there a php or javascript code that can detect the current user's page
How can I detect if a system supports hardware virtualization via code? (Preferably in
I'm trying to detect the compiler used to compile my source code. I can
How can I read a Chinese text file using C#, my current code can't
How can I detect the Windows OS versions in .NET? What code can I
I'm looking for .NET static code analysis tools that can detect things like unit
Can anyone can give me a JavaScript code snippet by which I can detect

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.