gpt4 book ai didi

java - 使用字符串填充 ArrayDeques 导致 NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-04 10:02:09 25 4
gpt4 key购买 nike

各位小白大家好。

我目前正在尝试编写一个修复后计算器程序,当我尝试清理冗余代码时,我发现根据我构建 ArrayDeque 的方式,即使数组内容相似,我的程序也会做出不同的 react 。我首先使用“.add()”重复填充我的 ArrayDeque,效果很好,但是我尝试使用字符串来清理它(这样我可以更有效地测试方程)所有这些都是在下面的测试类中完成的。我还留下了一些可能对你们有用的评论。

import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.Deque;

public class EvaluatorTest {
public static void main(String[] args) {
ArrayDeque < String > inFixEquation1 = new ArrayDeque < > ();
ArrayDeque < String > inFixEquation2 = new ArrayDeque < > ();

// Non DRY code: This for some reason doesnt work.
String equation = "A*(B+C)";
int temp = equation.length();
for (int i = 0; i < temp; i++) {
inFixEquation1.add(equation.substring(0, 1));
equation = equation.substring(1, equation.length());
}

// DRY code: While this does work.
inFixEquation2.add("A");
inFixEquation2.add("*");
inFixEquation2.add("(");
inFixEquation2.add("B");
inFixEquation2.add("+");
inFixEquation2.add("C");
inFixEquation2.add(")");

System.out.println("\nCreated inFix equation 1 = " + inFixEquation1);
System.out.println("Created inFix equation 2 = " + inFixEquation2);

Deque < String > postFixEquation = Evaluator.infixToPostfix(inFixEquation1); // I switch between the first and second inFixEquations here jsut by changing inFixeEquation1 to inFixEquation2
System.out.println("\nConverted inFix equation to postFix = " + postFixEquation);
System.out.println("postFix answer should equal = [A, B, C, +, *]");
BigInteger evaluated = Evaluator.evalPostfix(postFixEquation);
System.out.println("Evaulated postFix equation to BigInteger " +
"value = " + evaluated + "\n\nProgram end.");
}
}

我也为困惑的代码道歉,就像我说的,我正在清理代码。下面的代码是执行实际转换的类。

import java.math.BigInteger;
import java.util.*;

public class Evaluator {

public static Deque < String > infixToPostfix(Deque < String > in ) {
Deque < String > inFix = new ArrayDeque < String > ( in );
Deque < String > postFix = new ArrayDeque < > ();
Stack < String > storedOperators = new Stack();
Set < String > allOperators = new HashSet < >
(Arrays.asList("*", "/", "%", "+", "-", ")", "("));

for (int i = 0; i < in .size(); i++) {
if (!allOperators.contains(inFix.peek())) {
postFix.add(inFix.pop());
} else if (allOperators.contains(inFix.peek())) {
if (inFix.peek() == "(" || storedOperators.size() == 0 && inFix.peek() != ")") {
storedOperators.add(inFix.pop());
} else if (inFix.peek() == ")") { // The compiler seems to skip here when inFix.peek() equals ")" if I'm using the first ArrayDeque but not the second.
while (storedOperators.peek() != "(")
postFix.add(storedOperators.pop());
if (storedOperators.peek() == "(")
storedOperators.pop();
} else if (priorityCheck(inFix.peek(), storedOperators.peek())) {
while (inFix.size() > 0 && storedOperators.size() > 0 && priorityCheck(inFix.peek(), storedOperators.peek()))
postFix.add(storedOperators.pop());
storedOperators.add(inFix.pop());
} else if (!priorityCheck(inFix.peek(), storedOperators.peek())) {
storedOperators.add(inFix.pop());
}
}
}

for (int i = storedOperators.size(); i > 0; i--)
postFix.add(storedOperators.pop());
return postFix;
}

public static boolean priorityCheck(String inFix, String auxOp) {
boolean answer = false;
Map < String, Integer > opPriority = new HashMap < > () {
{
put("-", 1);
put("+", 2);
put("^", 3);
put("/", 4);
put("*", 5);
put("(", 1);
}
};
if (opPriority.get(inFix) < opPriority.get(auxOp))
answer = true;
return answer;
}
}

因此,如果我使用测试类中的第一个 ArrayDeque 方程运行,我会收到此错误,而第二个方程可以正常运行,你们知道这是为什么吗?我知道空指针来自哪里以及为什么它来自这个特定位置,但我想我主要想知道为什么我的程序没有捕获第二个类的第 19 行的最后一个“)”括号,当我手动构建没有 for 循环和字符串的 ArrayDeque 时它没有这样做。

Created inFix equation 1 = [A, *, (, B, +, C, )]
Created inFix equation 2 = [A, *, (, B, +, C, )]
Exception in thread "main" java.lang.NullPointerException
at Evaluator.priorityCheck(Evaluator.java:61)
at Evaluator.infixToPostfix(Evaluator.java:25)
at EvaluatorTest.main(EvaluatorTest.java:31)

最佳答案

opPriority映射缺少)运算符,您可以将)优先级设置为1,例如:

Map<String, Integer> opPriority = new HashMap<String, Integer>() {
{
put("-", 1);
put("+", 2);
put("^", 3);
put("/", 4);
put("*", 5);
put("(", 1);
put(")", 1);
}
};

然后 EvaluatorTest 输出如下:

Created inFix equation 1 = [A, *, (, B, +, C, )]
Created inFix equation 2 = [A, *, (, B, +, C, )]

Converted inFix equation to postFix = [A, *, B, C, +, ), (]
postFix answer should equal = [A, B, C, +, *]

已更新

对于inFixEquation1运行不正确的情况,是由比较两个字符串的方式引起的。

对于字符串值比较,您应该使用 oneStr.equals(otherStr) 而不是 ==== 表示比较字符串对象引用而不是字符串值。正确的做法如下:

public static Deque<String> infixToPostfix(Deque < String > in) {
Deque < String > inFix = new ArrayDeque< String >(in );
Deque < String > postFix = new ArrayDeque < > ();
Stack< String > storedOperators = new Stack();
Set< String > allOperators = new HashSet< >
(Arrays.asList("*", "/", "%", "+", "-", ")", "("));

for (int i = 0; i < in .size(); i++) {
if (!allOperators.contains(inFix.peek())) {
postFix.add(inFix.pop());
} else if (allOperators.contains(inFix.peek())) {
if (inFix.peek().equals("(") || storedOperators.size() == 0 && !inFix.peek().equals( ")")) {
storedOperators.add(inFix.pop());
} else if (inFix.peek().equals(")")) {
while (!storedOperators.peek().equals("("))
postFix.add(storedOperators.pop());
if (storedOperators.peek().equals("("))
storedOperators.pop();
} else if (priorityCheck(inFix.peek(), storedOperators.peek())) {
while (inFix.size() > 0 && storedOperators.size() > 0 && priorityCheck(inFix.peek(), storedOperators.peek()))
postFix.add(storedOperators.pop());
storedOperators.add(inFix.pop());
} else if (!priorityCheck(inFix.peek(), storedOperators.peek())) {
storedOperators.add(inFix.pop());
}
}
}

for (int i = storedOperators.size(); i > 0; i--)
postFix.add(storedOperators.pop());
return postFix;
}

关于java - 使用字符串填充 ArrayDeques 导致 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53349550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com