Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am writing a console program which takes an Infix expression ( such as "(a+b)" ) and displays RPN of that expression, well actually I am at the first phase of the program where I am checking if the given expression is parenthesized correctly or not.A NullRefereceException is being generated at expLength = expression.Length;. Why is the console not asking me to input my expression, thus resulting in a null value? I could not understand it from Google.
Thanks in advance!
The code is as in the following:
C#
static class Program
    {
        static void Main()
        {
            int squareB = 0, curlyB = 0, parenth = 0, expLength;
            string expression;
            //Get the Infix Expression
            System.Console.Write("Enter the Infix Expression : ");
            expression = System.Console.ReadLine();
            //Get the Length of the string
            expLength = expression.Length;
            //Checking expression
            for (int i = 0; i < expLength ; i++)
            {
                //Getting single character of the expression
                char expressionChar = expression[i];
                //Checking if Infix Expression is logically correct
                if (expressionChar == '[')
                    squareB++;
                if (expressionChar == ']')
                    squareB--;

                if (expressionChar == '{')
                    curlyB++;
                if (expressionChar == '}')
                    curlyB--;

                if (expressionChar == '(')
                    parenth++;
                if (expressionChar == ')')
                    parenth--;
            }
            if (squareB == 0)
                if (curlyB == 0)
                    if (parenth ==0)
                Console.Write("\n The Infix Expression is fully parentesized, RPN is being generated. ");
            else
                Console.Write("\n The Infix Expression is not fully parenthesized, hence RPN cannot be generated. ");

        }
    }
Posted
Updated 27-Nov-10 0:30am
v2
Comments
velvet7 27-Nov-10 6:13am    
I tried your code, and it works perfectly for me.

All you have to do to fix this is before using expresion, do this:

if (string.IsNullOrEmpty(expression))
{
  // parse your string here
}


And by the way, you parsing (as it stands right now) is going to give you a false valid if your brackets occur out of order, even if there are enough of them. So, this:

]bracket[ blah blah blah


will report valid, just like this will:

[bracket] blah blah blah
 
Share this answer
 
v5
C#
int squareB = 0, curlyB = 0, parenth = 0;
      string _sInputDetails; // Expression from user

      System.Console.CursorVisible = false;
      System.Console.ForegroundColor = ConsoleColor.DarkGreen;
      System.Console.BackgroundColor = ConsoleColor.Black;

   Begin: // Marker togo if we want more inputs.

      // Get the Infix Expression
      System.Console.Write("Enter the Infix Expression : ");

      _sInputDetails = System.Console.ReadLine();

      if (_sInputDetails.ToUpper().Trim() == "Q" ||
         _sInputDetails.ToUpper().Trim() == "QUIT")
      {
         Console.WriteLine("");
         Console.WriteLine("Good Bye");
         System.Threading.Thread.Sleep(1000); // Small Pause
         return;
      }

      // Checking expression

      for (int _iStepExpression = 0; _iStepExpression < _sInputDetails.Length; ++_iStepExpression)
      {
         // Research Array.FindAll();

         // Checking if Infix Expression is logically correct - More efficient as you dont bother with multiple condition validations.
         switch (_sInputDetails[_iStepExpression])
         {
            case '[':
               {
                  squareB++;

                  if(squareB > 0)
                     System.Console.Beep(squareB, 100);

                  break;
               }
            case ']':
               {
                  squareB--;

                  if (squareB > 0)
                     System.Console.Beep(squareB, 100);

                  break;
               }
            case '{':
               {
                  curlyB++;

                  if (curlyB > 0)
                     System.Console.Beep(curlyB, 100);

                  break;
               }
            case '}':
               {
                  curlyB--;

                  if (curlyB > 0)
                     System.Console.Beep(curlyB, 100);

                  break;
               }
            case '(':
               {
                  parenth++;

                  if (parenth > 0)
                     System.Console.Beep(parenth, 100);

                  break;
               }
            case ')':
               {
                  parenth--;

                  if (parenth > 0)
                     System.Console.Beep(parenth, 100);

                  break;
               }
         } // switch (_sInputDetails[_iStepExpression])
      } // for (int _iStepExpression = 0; _iStepExpression < _sInputDetails.Length; ++_iStepExpression)

      string _sOutputResult = string.Empty;

      if (squareB == 0)
         if (curlyB == 0)
            if (parenth == 0)
               _sOutputResult =
                  System.Environment.NewLine +
                  "The Infix Expression is fully parentesized, RPN is being generated.";
            else
               _sOutputResult =
                  System.Environment.NewLine +
                  "The Infix Expression is not fully parenthesized, hence RPN cannot be generated.";

      Console.WriteLine(_sOutputResult);
      Console.WriteLine("");
      goto Begin;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900