使用测试线束时出现Java控制台错误消息

使用测试线束时出现Java控制台错误消息,java,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();

我试图做关于正则表达式的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工具选项:-javaagent:/usr/share/JAVA/jayatanaag.jar 没有控制台

它正在导入“控制台”并在我的控制台上显示错误。所以我不知道为什么它没有创建控制台


该页面可在以下位置找到:

这里您有使用适合Java编译器的扫描仪的代码 (我的月食):


这里是使用适合Java编译器的扫描器的代码 (我的月食):


您可以使用以下命令。它还有while循环,所以你可以毫不费力地练习你的正则表达式

package regex;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTestHarness {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.printf("%nEnter your regex: ");
            Pattern pattern = Pattern.compile(in.nextLine());

            System.out.printf("Enter input string to search: ");
            Matcher matcher = pattern.matcher(in.nextLine());

            boolean found = false;
            while (matcher.find()) {
                System.out.printf("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) {
                System.out.printf("No match found.%n");
            }
            // in.close();
        }
    }
}

您可以使用以下命令。它还有while循环,所以你可以毫不费力地练习你的正则表达式

package regex;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTestHarness {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.printf("%nEnter your regex: ");
            Pattern pattern = Pattern.compile(in.nextLine());

            System.out.printf("Enter input string to search: ");
            Matcher matcher = pattern.matcher(in.nextLine());

            boolean found = false;
            while (matcher.find()) {
                System.out.printf("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) {
                System.out.printf("No match found.%n");
            }
            // in.close();
        }
    }
}

您是否在eclipse下运行应用程序?如果是这样,请使用java.util.Scanner,因为System.console()返回null(eclipse错误:)您是否在eclipse下运行应用程序?如果是这样,请使用java.util.Scanner,因为System.console()返回null(eclipse错误:)