【问题标题】:Java console error message while using test harness使用测试工具时出现 Java 控制台错误消息
【发布时间】:2018-01-19 15:47:48
【问题描述】:

我正在尝试编写有关正则表达式的 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

【问题讨论】:

标签: java


【解决方案1】:

这里有使用适用于 Java 编译器的 Scanner 的代码 (对我来说日食):

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

/*
 * Test for regular expressions - Test Harness
 * List of commands and more info:
 * https://docs.oracle.com/javase/tutorial/essential/regex/test_harness.html
*/
public class RegexTestHarness {

    public static void main(String[] args){

            Scanner in = new Scanner(System.in);

            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();
        }

}

【讨论】:

    【解决方案2】:

    您可以使用以下内容。它也有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();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 2020-08-10
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      • 2012-05-02
      • 2018-04-15
      相关资源
      最近更新 更多