0
식을 중위에서 후방으로 변환하려고합니다. 코드 디버깅을 시도했지만 변수가 팝되면 null 포인터 예외가 계속 발생합니다. I는 오류가있는 입력은 :접두사에서 중위 어로의 변환 : 널 포인터 예외
(=> (NOT (상)) (badgrade))
I 오류가 (NOT 상)
이다 얻을 후에 출력작은 코드 게시/더 많은 의견 추가/더 많은 정보 제공을 위해 Q를 편집해야하는지 알려주십시오. 감사!
public class toInfix1 {
Stack<String> symbol = new Stack<String>();
Stack<String> variable = new Stack<String>();
Stack<String> operator = new Stack<String>();
static String inputfile = "kb2.txt";
ArrayList<String> infix = new ArrayList<String>();
public void toPrefix1() {
try {
File f = new File(inputfile);
FileReader fr = new FileReader(f);
BufferedReader bf = new BufferedReader(fr);
String str;
String kb = "";
while ((str = bf.readLine()) != null) {
Pattern pattern = Pattern.compile("[(]|[)]|<=>|=>|\\w+|^\\s+");
Matcher m = pattern.matcher(str);
//int a = 0;
System.out.println("KB" + kb);
while (m.find()) {
String node1 = m.group();
System.out.println("Node1" + node1);
//If (
if (node1.equals("(")) {
symbol.push(node1);
} else if (node1.equals("OR") || node1.equals("AND")
|| node1.equals("NOT") || node1.equals("=>")
|| node1.equals("<=>")) {
operator.push(node1);
//If)
} else if (node1.equals(")")) {
//Pop symbol (
if(!variable.empty()&& !operator.empty() && !symbol.empty()){
String symbol1 = "", op = "";
if (symbol.peek() != null && !symbol.empty()) {
symbol1 = symbol.pop();
}
//Pop if operator AND OR => <=> (Binary)
if (operator.peek() != null && !operator.empty()) {
op = operator.pop();
if (op.equals("AND") || op.equals("OR")
|| op.equals("=>") || op.equals("<=>")) {
String var2 = "";
String var1 = "";
if (variable.peek() != null && !variable.empty()) {
var1 = variable.pop();
}//Error occurs in the following if condition
if (variable.peek() != null && !variable.empty()) {
var2 = variable.pop();
}
kb = "(" + var1 + op + var2 + ")";
variable.push(kb);
//Pop if operator NOT (Unary)
} else if (op.equals("NOT")) {
String var1 = "";
if (variable.peek() != null && !variable.empty()) {
var1 = variable.pop();
}
kb = "(" + op + var1 + ")";
variable.push(kb);
//No operator after popping)
}
}
}
//If there are no operators
} else {
variable.push(node1);
}
}
}
fr.close();
} catch (Exception e) {
System.err.println("Error thrown" + e.getMessage());
}
}
public static void main(String[] args) {
System.out.println("In new file");
toInfix1 inf1 = new toInfix1();
inf1.toPrefix1();
System.out.println("Completed");
}
}
모든 3 스택의 스택 추적을 확인한 결과 올바른 변수로 올바른 것으로 나타났습니다. 그러나 ** ** variable.empty()를 확인하는 코드 행은 중단 점의 행입니다. 스택이 비었지만이 줄 뒤에 코드가 실행되지 않는 이유는 알 수 없습니다. –
전체 코드 목록을 넣으면 좋을 것입니다. – sanbhat
출력을 원하십니까? 코드 목록이란 무엇입니까? 미안, 나는 그것을 얻지 않았다. –