Search code examples
javacompiler-constructioncompilation

Why is matcher.group giving me an error?


I am trying to compile the following while loop, but I keep getting a red flag in eclipse next to the group.matcher("...") that says to remove the argument so i match group.

The error i am getting is the following:

The method group(int) in the type Matcher is not applicable for the arguments (String)

The method group(int) in the type Matcher is not applicable for the arguments (String)

The method group(int) in the type Matcher is not applicable for the arguments (String)

The method group(int) in the type Matcher is not applicable for the arguments (String)

The method group(int) in the type Matcher is not applicable for the arguments (String)

Here is the code:

while (matcher.find()) {
        if (matcher.group(TokenType.NUMBER.name()) != null) {
            tokens.add(new Token(TokenType.NUMBER, matcher.group(TokenType.NUMBER.name())));
            continue;
        } else if (matcher.group(TokenType.BINARYOP.name()) != null) {
            tokens.add(new Token(TokenType.BINARYOP, matcher.group(TokenType.BINARYOP.name())));
            continue;
        } else if (matcher.group(TokenType.WHITESPACE.name()) != null)
            continue;
    }

Solution

  • Since I assume that you use Java 6 or below, matcher.group takes only a int as parameter. If you want to use matcher.group(String) upgrade your project to Java 7. See http://docs.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html#group(java.lang.String) and http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#group(java.lang.String)