我正在尝试编写一个用' +'来打破字符串的程序。标志。例如,如果我的输入是" 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");
}