Daniil Shevelev
Daniil Shevelev

Reputation: 12027

Groovy "java.lang.Integer.readLine() is applicable for argument types: () values: []" error

I am trying to write a brief code that would read a few lines from console. This is my code:

System.in.withReader {
    int a = it.readLine() as int
    (1..a).each {
        int b = it.readLine() as int
        def sum = 0
        (0..(b-1)).each {d ->
            sum+=(-1)^d/(2*d+1)
        }
        println sum/4
    }
}

This is input from the console:

1
20

And this is the error I get:

java.lang.Integer.readLine() is applicable for argument types: () values: []

I have a feeling that somehow Groovy does not get inputs from console. When I tried debugging it did not allow me to enter anything into console.

Upvotes: 0

Views: 1098

Answers (1)

cfrick
cfrick

Reputation: 37033

Your first each shadows the it from the withReader:

System.in.withReader { /* implicit it */
    int a = it.readLine() as int
    (1..a).each { /* implicit it */
        int b = it.readLine() as int // this `it` now is an int from (1..a)

Give the inner it a name (like you do later with d) and the original it will remain the reader. To further untangle this you might even want to give the reader its own var name.

Upvotes: 1

Related Questions