0

This question has a follow-up question here.


Following this tutorial and compiling the given RegexTestHarness is giving the following errors on console.readLine(String) and console.Format(String), respectively:

  1. The method readLine() in the type Console is not applicable for the arguments (String)

  2. The method format(String, Object[]) in the type Console is not applicable for the arguments (String, String, int, int)

According to the documentation, there are two arguments required there:

  • public String readLine(String fmt, Object... args)

  • public Console format(String fmt, Object... args)

The second argument of type Object for both the methods is:

  • args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by.

So I believe that it changed after the tutorial was published.

QUESTION:-

What is meant the arguments referenced by the format specifiers?

Firstly I thought that it is the format specifiers themselves but then I am also getting an error on Matcher matcher = pattern.matcher(console.readLine("Enter input string to search: ")); statement.


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

/*
 *  Enter your regex: foo
 *  Enter input string to search: foo
 *  I found the text foo starting at index 0 and ending at index 3.
 * */

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: ")); //********ERROR*****

            Matcher matcher = 
            pattern.matcher(console.readLine("Enter input string to search: ")); //********ERROR*****

            boolean found = false;
            while (matcher.find()) {
                console.format("I found the text" +   //********ERROR*****
                    " \"%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");  //********ERROR*****
            }
        }
    }
}

enter image description here

Community
  • 1
  • 1
Solace
  • 8,612
  • 22
  • 95
  • 183
  • Please ask one question at a time and provide any relevant code *in context* instead of as a single line. – Ant P Jun 15 '14 at 08:49
  • @AntP Did I ask more than one question? – Solace Jun 15 '14 at 09:11
  • 1
    "According to the documentation, there are two arguments required there" - This is not correct. What is required is: at least one argument of type String. Optionally, and depending on the formats used (or not used) in the first argument, 0 or more arbitrary objects may be required. – laune Jun 15 '14 at 10:39

2 Answers2

3

From the JavaDoc for Console:

Provides a formatted prompt, then reads a single line of text from the console.

How does this work? Well, it uses a format string with parameters. The parameters are a varargs array so you can pass none or many without any special syntax.

For example

console.readLine("No arguments");

Will just output "No arguments" onto the prompt.

final String a = "A";
console.readLine("With string, %s", a);

Will output "With string, A" onto the prompt.

final String a = "A";
final int b = 10;
console.readLine("With string %s and a formatted number %.2f", a, b);

Will output "With string A and a formatted number 10.00" onto the prompt.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • Thank you very much for the extensive explanation. You explained it very well, but the statement `Matcher matcher = pattern.matcher(console.readLine("Enter input string to search: "));` resembles the first example you have given in that it does not contain any format specifiers. But I am still getting the same error on this statement. – Solace Jun 15 '14 at 09:10
  • @Zarah The code you have posted compiles just fine. I haven't run it. What JDK are you compiling it on? `Console` was introduced in 1.6 and it has always been varargs. – Boris the Spider Jun 15 '14 at 10:04
  • The return value of readLine() should normally be stored in a String variable. – laune Jun 15 '14 at 10:35
  • @BoristheSpider I am using jDK 1.8.0_20, I am getting these errors, I just posted a screenshot in the question. – Solace Jun 15 '14 at 11:55
  • @laune `Pattern.compile` is taking the string returned by `readLine()` as its argument. `Pattern.matcher` takes a `charSequence` as an argument. I am newb (not aware of relationship between charSequence and String in Java, though they should generally be the same thing) and I am assuming that the `string` returned by `readLine()` in the later case is internally being converted to a `charSequence` – Solace Jun 15 '14 at 12:01
  • @Zarah you don't happen to have another class called `Console` in the same package as your class do you? – Boris the Spider Jun 15 '14 at 20:42
  • @BoristheSpider No, I don't. `java.io.Console` a built-in class right? I did import it. – Solace Jun 16 '14 at 03:04
  • But my virtual machine does not seem to have a console device. – Solace Jun 16 '14 at 03:11
1

A format string (mostly) contains things like %d or %s and these items should correspond to expressions following the format string in the method call: these are the "referenced arguments".

What error did you get for the Pattern/Matcher call?

laune
  • 31,114
  • 3
  • 29
  • 42
  • The error mentioned in the question is in `readLine()`, which is the argument(s) of the `Pattern.compile()` and `pattern.matcher()` calls in the following statements: `Pattern pattern = Pattern.compile(console.readLine("%nEnter your regex: "));` ` Matcher matcher = pattern.matcher(console.readLine("Enter input string to search: "));` – Solace Jun 15 '14 at 09:03
  • @Zarah That much was clear to me - What was the error (NOT WHERE)? – laune Jun 15 '14 at 10:27
  • Oh, sorry, my bad! But the error(s) are given in the question, numbered 1 and 2, in the very beginning of the question. According to the error, readLine() and format() need another argument of type Object, apart from the String argument. I got this error on the statement which does not have a format specifier (%d, %s etc.) in its string: `Matcher matcher = pattern.matcher(console.readLine("Enter input string to search: "));` – Solace Jun 15 '14 at 11:45
  • @Zarah As others already have commented, you must be using some pretty old Java version in your IDE. Upgrade! – laune Jun 15 '14 at 12:13
  • I don't think JDK1.8.0_20 and a compiler compliance of 1.7 is that old :s – Solace Jun 15 '14 at 12:51
  • 1
    Maybe you can compile that code by invoking javac/JDK1.8.0 from the command line. If it compiles, it is proof that something is not quite right in your IDE. When I call `console.readLine( intergerObject, "string" )`, the error message reads "method Console.readLine(String,Object...)" - note the **Object...**. But your IDE displays **Object[]**. – laune Jun 15 '14 at 13:02