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: