私は後置変換器/電卓への中置を書くことに取り組んでいます。入力ファイルから読み取り、読み取った文字列と一致させようとしています。テストに使用された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;
}