import java.io.Console;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReguxTestingHarness {
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");
}
}
}

}
执行上面的代码,发现在Eclipse里面报No console,在cmd里面执行就ok
JDK是1.6的,是因为Eclipse本来自己就有console了,所以不work嘛?只能用readerbuffer来读?
谢谢啦

解决方案 »

  1.   

    不能这么用,用scanner和system.in,你查查
      

  2.   

    Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.”