1

I'm trying to learn Regular Expressions in JAVA, it was suggested I copy and compile code in my preferred IDE (Eclipse) to test how the API works.
Source: https://docs.oracle.com/javase/tutorial/essential/regex/test_harness.html

When I run, my IDE simply says "No Console" in the output.

I've written many programs from the online classes I've been taking for JAVA, never encountering a situation where the console is not recognized. I have exported those compiled projects as runnable .jars and never had a problem from the command line executing only the jar file name. I have found when exporting as a runnable .jar - for this specific jar file - to preface executing on the command line with --> java -jar <*runnable.jar*>.
That works ... running from my IDE does not.
Perhaps obviously, I'm a newbie to OOP, and I've searched everywhere (including on your site), and don't have a clue. I am running the Mars 2 (4.5.2) version of Eclipse on a Windows 7 64-bit machine; JRE/JDK 8; along with a JAVA_HOME ENV setup.
Can someone give me an idea as to which properties to change in the IDE settings for Eclipse? Or perhaps, the Oracle code needs to be augmented for my particular environment?

Hugues M.
  • 19,846
  • 6
  • 37
  • 65
MSGILCH
  • 11
  • 2
  • 1
    `System.console()` requires a terminal, that code is not meant to be run in an IDE, that's a shame because it could very well read from stdin & print to stdout instead... – Hugues M. Jun 23 '17 at 16:54
  • The name of the programming language is "Java", not "JAVA". – Lew Bloch Jun 23 '17 at 18:06
  • @ Bloch --> I was going by Cay Horstmann's spelling but, I'll keep that in mind going forward. – MSGILCH Jun 26 '17 at 12:13

1 Answers1

0

This tutorial uses System.console(), but this requires an actual terminal, and that won't work when running in an IDE. That's a shame because it could very well read from System.in & print to System.out instead.

Here is a replacement that will work in Eclipse or any good IDE:

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

public class RegexTestHarness {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.println("\nEnter your regex: ");
            Pattern pattern =
                    Pattern.compile(input.nextLine());

            System.out.println("Enter input string to search: ");
            Matcher matcher =
                    pattern.matcher(input.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.println("No match found.\n");
            }
        }
    }
}
Hugues M.
  • 19,846
  • 6
  • 37
  • 65