0

I'm working on a Reverse Polish Notation program, and I'm having issues with dealing with an invalid expression... I can deal with it if it has too few operators or operands, but I can't deal with having any characters that aren't "(", "+", "-", "*", and "#".

Basically I want to know of a way to skip the rest of the line (or force a pound symbol into the input) instead of reading the whole expression.

Here's my code below:

public class RpnEvaluator
{
   private Scanner stdin;
   private Stack stack = new Stack(50);
   private Queue queue = new Queue(50);
   private String expression = "";
   private String interValue = "";
   private int numExpressions = 0;

   /**
   Runs the RPN Evaluator.
   */
   public void run()
   {  
      stdin = new Scanner(System.in);
      while( stdin.hasNext() )
      {
         String input = stdin.next();
         if( input.charAt(0) == '(' )
            addOperand(input);
         else if( input.equals("+") || input.equals("-") || input.equals("*") )
            performOperation(input.charAt(0));
         else if( input.equals("#") )
            outputExpression();
         else
            invalidExpression(); **// Here is where I need to deal with anything that isn't above and output anything BEFORE the bad value, AND the value itself.**

      }
      System.out.println("Normal Termination of Program 3.");
   }

For example: An input such as

(2/9) B (4/3) / # 

should return this output:

Expression 1 is: (2/9)B
Invalid Expression
Intermediate results:
user207421
  • 305,947
  • 44
  • 307
  • 483
liquidsystem
  • 634
  • 5
  • 15
  • 2
    Nit: don't say "pound symbol" if you mean `#` (like you use previously): as a Brit, I would otherwise think you mean [£](http://www.fileformat.info/info/unicode/char/a3/index.htm). – Andy Turner Oct 14 '15 at 20:58
  • Store the inputs so that you can print everything leading up to the bad character when you come across one. Try to fill out `invalidExpression()` yourself and see what you get. – Evan LaHurd Oct 14 '15 at 20:59
  • So the inputs are automatically parsed as either an operator, or operand, and placed in a Stack for later use, but we aren't supposed to place bad tokens on the stack, I'm just wondering how to deal with it. – liquidsystem Oct 14 '15 at 21:01
  • Just print it as an error and throw it away: keep going with the parse. You don't want stop on the first error, you want to report them all. NB you need to look up recursive descent expression parsing, or the Dijkstra Shunting-yard algorithm. This code doesn't handle operator precedence correctly, and it will never handle parentheses. – user207421 Oct 14 '15 at 21:03
  • We have to follow specific ways of doing things in the class, he'd most likely take off points for not doing it a certain way, the only problem is: I don't know what way he expects us to do it. EDIT: I need to know a way to skip to the # symbol and if there is more expressions after that, deal with it. – liquidsystem Oct 14 '15 at 21:09
  • He's told you to treat parentheses as operands? Hard to believe. – user207421 Oct 14 '15 at 21:09
  • No, sorry I may have confused it again, when you read the first character as a (, you go down to the constructor for a fraction, that parses "()/" as a delimiter and creates the fraction from the numerator/denominator. I figured it out so I'll post my code. – liquidsystem Oct 15 '15 at 15:29

2 Answers2

0

After looking over the notes, I found a section where we first introduced Reverse Polish Notation and we actually talked about dealing with possible errors, from those notes, I created these two methods that seem to do the trick perfectly.

private void skipExpression()
{
   while( stdin.hasNext() )
    {
      input = stdin.next();
      if( input.equals("#") )
         break;
   }
}

private void invalidExpression()
{
   skipExpression();
   numExpressions++;
   System.out.println("Expression " + numExpressions + " is: " + expression);
   System.out.println("Invalid Expression");
   System.out.println("Intermediate results: " + interValue);
   expression = "";
   interValue = "";
   stack.clear();
   queue.clear();
}
liquidsystem
  • 634
  • 5
  • 15
0

I would first test with hasNext(Pattern pattern) if the following token is valid with a pattern like [0-9+/*\-#] or one more context-specific, and if not I would skip(Pattern pattern) with a pattern matching every character but carriage return, which . does

Aaron
  • 24,009
  • 2
  • 33
  • 57
  • Thank you for your response, however we haven't talked about using `.hasNext()` with any form of parameters, the answer I gave below did the trick, even if it isn't the best solution. – liquidsystem Oct 15 '15 at 16:46