0
我正在嘗試編寫一個程序,用'+'符號來打斷字符串。例如,如果我的輸入是「1 + 2 + 3 * 4」。該程序將打印1,2,3 * 4。我用\ s * \ + \ s *作爲我的模式。但是,它與模式匹配時不會打印出來?沒有正確打印
private Scanner kbd = new Scanner(System.in);
private String tokenPattern = "\\s*\\+\\s*"; //pattern
public void repl() {
while(true) {
try {
System.out.print("-> ");
String input = kbd.nextLine();
if (input.equals("quit")) break;
Scanner tokens = new Scanner(input);
while(tokens.hasNext(tokenPattern)) { //figure out why its not printing
String token = tokens.next(tokenPattern);
System.out.println(token);
}
tokens.close();
} catch(Exception e) {
System.out.println("Error, " + e.getMessage());
}
}
System.out.println("bye");
}
嘗試使用this.split。這會更容易 –