我正致力于将后缀写入postfix翻译器/计算器。我正在读取输入文件并尝试匹配读取的字符串。我知道我传入方法正确的字符串,如用于测试的print语句所见..我只是想弄清楚为什么不满足if语句的条件!
static int j = 1;
public static void readMath(String str, myStack s, myQueue q) {
System.out.println("\n~~~round "+j+"~~~ str=="+str);//<--this line confirms that the correct string is being passed in
//for example: if "1" is passed in, the first if statement's conditions are failing to be met
j++;
if (str == "0" || str == "1" || str == "2" || str == "3" || str == "4" || str == "5" || str == "6" || str == "7" || str == "8" || str == "9") {
System.out.println(">NUMBER"); // <--for testing.
q.enqueue(str);
} else if(str == "+" || str=="-") {
System.out.println("> + or -");
String x = (String)s.pop();
String y = x;
while( !s.isEmpty() && !(x == "<" || x == ">" || x == "&" || x == "|" || x =="=")) {
q.enqueue(x);
x = (String) s.pop();
}
if(x == "<" || x == ">" || x == "&" || x == "|" || x == "=") {
q.enqueue(x);
s.push(y);
}
} else if(str == "<" || str == ">") {
System.out.println(">GT or LT"); // <--for testing.
String x = (String) s.pop();
String y = x;
while( !s.isEmpty() && !(x == "&" || x == "|" || x == "=")) {
q.enqueue(x);
x = (String) s.pop();
}
if(x == "&" || x == "|" || x == "=") {
q.enqueue(x);
s.push(y);
}
} else if(str == "=") {
System.out.println("> ="); // <--for testing.
String x = (String) s.pop();
String y = x;
while( !s.isEmpty() && !(x == "&" || x == "|")) {
q.enqueue(x);
x = (String) s.pop();
}
if(x == "&" || x == "|") {
q.enqueue(x);
s.push(y);
}
} else if(str == "&" || str == "|") {
System.out.println("> & or |"); // <--for testing.
String x = (String) s.pop();
String y = x;
while( !s.isEmpty() && !(x == "!" || x == "&" || x == "|")) {
q.enqueue(x);
x = (String) s.pop();
}
} else if(str=="/" || str == "*") {
System.out.println(">divide or multiply"); // <--for testing.
String x = (String) s.pop();
String y = x;
while( !s.isEmpty() && !(x == "&" || x == "|" || x == "=" || x == "<" || x == ">" || x == "+" || x == "-")) {
q.enqueue(x);
x = (String) s.pop();
}
if(x == "&" || x == "|" || x == "=" || x == "<" || x == ">" || x == "+" || x == "-") {
q.enqueue(x);
s.push(y);
}
} else if(str == ")") {
System.out.println(">close paren"); // <--for testing.
String x = (String) s.pop();
while( !s.isEmpty() && x != "(" ) {
q.enqueue(x);
x = (String) s.pop();
}
}
s.printStack();
q.printQueue();
}
public static myStack s;
public static myQueue q;
public static void readMathFile() {
s = new myStack();
q = new myQueue();
File afile = new File ("/Users/tteumer2010/Documents/java/Project1/src/test.txt");
FileReader fileread = null;
try { fileread = new FileReader(afile); }
catch (FileNotFoundException e) { e.printStackTrace(); }
BufferedReader bufread = new BufferedReader(fileread);
String str = new String();
try {
while((str = bufread.readLine()) != null) {
String[] a = parse(str);
for(int i = 0; i < a.length; i++) {
System.out.println(a[i]);
readMath(a[i], s, q);
}
}
} catch (IOException e) { e.printStackTrace(); }
}
public static String[] parse(String s) {
String[] str = s.split(" ");
return str;
}
答案 0 :(得分:4)
另一个字符串平等问题,在这里你去......
(str == "0" and all the string equality compariosions in your code
应该是
("0".equals(str)
使用equals()
方法检查字符串相等性。==
运算符检查两个字符串引用是否引用相同的字符串对象。
答案 1 :(得分:3)
或者更确切地说使用"0".equals(str)
代替str.equals("0")
,因为如果str为null,则最后一个将失败(NullPointerException)
干杯!