简体   繁体   English

如果收到错误字符,Java扫描仪将停止读取输入

[英]Java Scanner stop reading input if receives bad character

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:

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();
}

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 . 我首先使用hasNext(Pattern pattern)测试以下标记是否与[0-9+/*\\-#]类的模式有效,或者与特定上下文相关,否则,我将使用a跳过(Pattern模式)匹配除回车符()以外的每个字符的模式. does 确实

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM