Java 打印不正确
我正试图写一个程序,用“+”符号来打断字符串。例如,如果我的输入是“1+2+3*4”。程序将打印1,2,3*4。我使用\s*\+\s*作为我的模式。但是,当它与模式匹配时不会打印出来Java 打印不正确,java,Java,我正试图写一个程序,用“+”符号来打断字符串。例如,如果我的输入是“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 {
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。这样会容易些