Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检测表达式字符串中的运算符和操作数错误_Java - Fatal编程技术网

Java 检测表达式字符串中的运算符和操作数错误

Java 检测表达式字符串中的运算符和操作数错误,java,Java,我一直在试图找出如何检测正则中缀表达式中发生的位置和错误。我想到的第一件事是这样的 String expression = "(12 * 12) + (12 * 9)"; int numOfDigits = 0; int numOfOperators = 0; boolean onDigit = false; for (int i = 0; i < expression.length(); i++) { char ch = expression.charAt(i)

我一直在试图找出如何检测正则中缀表达式中发生的位置和错误。我想到的第一件事是这样的

String expression = "(12 * 12) + (12 * 9)";
int numOfDigits = 0;
int numOfOperators = 0;
boolean onDigit = false;

for (int i = 0; i < expression.length(); i++) {
  char ch = expression.charAt(i);

  if (Character.isDigit(ch) && !onDigit) {
    numOfDigits += 1;
    onDigit = true;
  } else if (ch == '+' || ch == '-' || ch == '/' || ch == '*') {
    numOfOperators += 1;
    onDigit = false;
  } else if (Character.isWhitespace(ch)) {
    onDigit = false;
  }
}

if (numOfDigits - 1 != numOfOperators) {
  System.out.println("Missing operand(s) or operator(s) in expression");
}
String expression=“(12*12)+(12*9)”;
整数位数=0;
int numOfOperators=0;
布尔onDigit=false;
for(int i=0;i
唯一的问题是,我无法检测到错误来自表达式的何处。在这一点上,我决定使用类似这样的东西,因为它可以检测错误来自字符串中的什么地方。我唯一的问题是,如果字符串的末尾有一个操作符,我就不知道如何让程序检测到它并显示错误。任何帮助都将不胜感激

for (int i = 0; i < expression.length(); i++) {
  char ch = expression.charAt(i);

  if (lastType.equalsIgnoreCase("") && (ch == '+' || ch == '-' || ch == '/' || ch == '*')) {
    System.out.printf("Missing operand at: %d\n", lastLocation + 1);
    break;
  }

  if (Character.isDigit(ch)) {
    if (!onDigit) {
      if (lastType.equalsIgnoreCase("digit")) {
        System.out.printf("Missing operator at: %d\n", lastLocation + lenOfDigit);
        break;
      }
      lastType = "Digit";
      lastLocation = i + 1;
      numOfDigits += 1;
      onDigit = true;
    }
    lenOfDigit++;
  } else if (ch == '+' || ch == '-' || ch == '/' || ch == '*') {
    if (lastType.equalsIgnoreCase("operator")) {
      System.out.printf("Missing operand at: %d\n", lastLocation + 1);
      break;
    }
    lastType = "Operator";
    lastLocation = i + 1;
    numOfOperators += 1;
    onDigit = false;
    lenOfDigit = 0;
  } else if (Character.isWhitespace(ch)) {
    onDigit = false;
  }
}
for(int i=0;i
您可以向后遍历字符串并找到
Character.isDigit()
true的第一个索引。然后同时查找字符为运算符的第一个索引。如果运算符的索引大于数字的索引,则发现了运算符是表达式中最后一个字符的差异

编辑:

我假设您希望检测表达式是否有后跟任何数字的运算符

例如:
“(12*12)+(12*9)+”

您可以通过以下方式检测到这一点:

int lastDigitIndex = -1;
int lastOperatorIndex = -1;

for (int i = expression.length()-1; i >= 0; --i)
{
    char ch = expression.charAt(i);

    if (Character.isDigit(ch) && lastDigitIndex == -1)
        lastDigitIndex = i;
    else if((ch == '+' || ch == '-' || ch == '/' || ch == '*') && lastOperatorIndex == -1)
        lastOperatorIndex = i;

    if (lastDigitIndex != -1 && lastOperatorIndex != -1)
        break;  
}

if (lastOperatorIndex > lastDigitIndex)
{
    //Error found
}

你能解释一下你的答案吗?如果这个答案有帮助,请考虑把它标记为这样。如果没有,请随时告诉我。唯一的问题是它没有检测到问题发生在字符串中的什么位置。通过我的尝试,我让它检测到了,只是如果现场有一名操作员,它就不会检测到end@AlexLowe问题是不能保证它会检测到任何东西。这只是一段特殊的代码,不是根据任何公认的原则编写的。不要使用。抛弃就是一切,使用适当的表达式解析器,例如递归下降或Dijkstra调车场算法,没有替代品。手工制作的方法不行。你还有什么建议可以作为出发点吗?我已经提出了两个一般建议和两个具体建议。为什么你现在要求我提供更多?我只是想弄清楚如何实现它们这些都是标准技术。在维基百科上查找它们。这里也有很多关于它们的问题和答案。