2016-01-21 42 views
1

我正在嘗試使用正則表達式的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

+0

你在eclipse下運行你的應用程序嗎?如果這樣使用java.util.Scanner是因爲System.console()返回null(eclipse bug:https://bugs.eclipse.org/bugs/show_bug.cgi?id = 122429) – hasnae

+0

它在eclipse中 – user3137110

回答

0

在這裏,你可以選擇使用適合一個Java compilator (日食對我來說)掃描儀代碼:

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

} 
0

您可以使用以下。它也有while循環,所以你可以毫不費力地練習你的regex。

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