我正在尝试使用正则表达式的java教程,它有一个测试工具。我复制了代码并试图运行它。使用测试工具时的Java控制台错误消息
import java.io.Console;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class RegexTestHarness {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.err.println("No console.");
System.exit(1);
}
while (true) {
Pattern pattern = Pattern.compile(console.readLine("%nEnter your regex: "));
Matcher matcher = pattern.matcher(console.readLine("Enter input string to search: "));
boolean found = false;
while (matcher.find()) {
console.format("I found the text" +
" \"%s\" starting at " +
"index %d and ending at index %d.%n",
matcher.group(),
matcher.start(),
matcher.end());
found = true;
}
if (!found) {
console.format("No match found.%n");
}
}
}
}
我收到以下错误消息,我的控制台上
拿起JAVA_TOOL_OPTIONS:-javaagent:/usr/share/java/jayatanaag.jar 没有控制台。
它导入“控制台”并在控制台上显示错误。所以我不确定它为什么不创建控制台。
的页面,可以发现在:JAVA test Harness
你在eclipse下运行你的应用程序吗?如果这样使用java.util.Scanner是因为System.console()返回null(eclipse bug:https://bugs.eclipse.org/bugs/show_bug.cgi?id = 122429) – hasnae
它在eclipse中 – user3137110