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