我正在尝试构建一个在使用 Split 函数后读取某个字符串的程序
import java.util.Scanner;
public class Lexa2 {
public void doit() {
String str = "( 5 + 4 ) * 2";
String [] temp = null;
temp = str.split(" ");
dump(temp);
}
public void dump(String []s) {
for (int i = 0 ; i < s.length ; i++) {
if (s[i] == "(") {
System.out.println("This is the left paren");
} else if (s[i] == ")"){
System.out.println("This is the right paren");
}else if (s[i] == "+"){
System.out.println("This is the add");
}else if (s[i] == "-"){
System.out.println("This is the sub");
}else if (s[i] == "*"){
System.out.println("This is the mult");
}else if (s[i] == "/"){
System.out.println("This is the div");
}else
System.out.println("This is a number");
}
}
public static void main(String args[]) throws Exception{
Lexa2 ss = new Lexa2();
ss.doit();
}
}
输出应该是这样的:
This is the left paren
this is a number
this is the add
this is the right paren
this is a number