-1

When I executed the following code using the built-in Groovy tool in IntelliJ IDEA, an error occurred.

StringBuilder sb = new StringBuilder()

String a = "abc"

String b = "123"


for (int i = 0, j = 0; i < a.length() || j < b.length(); i++, j++) {


    if (i < a.size()) {
        print(a[i])

        sb.append(a[i])
    }

}

print(sb)

Error is as follows

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
ideaGroovyConsole.groovy: 8: unexpected token: = @ line 8, column 12.
   for (int i = 0, j = 0; i < a.length() || j < b.length(); i++, j++) {
              ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)
    at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)
for (int i = 0, j = 0; i < a.length() || j < b.length(); i++, j++) {

My guess is that the grammar problems, but didn't send any questions。

1
  • 2
    "I use the idea of groovy code execution,why?" this subject doesn't make sense. Please update it. Commented Jun 27, 2023 at 11:56

1 Answer 1

0

What version of Groovy do you use? At some point Groovy didn't support initialization of more than one variable in the first part of a for loop. See more: https://stackoverflow.com/a/27386418/1842599

I've checked with Groovy 4 (I have 4.0.8 installed), multiple initialization works like a charm.

So, you may consider upgrading your Groovy version or rewrite your code, if upgrade is not possible.

Sign up to request clarification or add additional context in comments.

5 Comments

It is highly likely that the issue is related to the Groovy version. My Groovy version is 2.4.5.
@Daniel then it's 100% it. Groovy 2 doesn't support it. Install Groovy 4 and check, it should work.
I upgraded the groovy version to 4.0.7 it woks now . I can't vote for i am a novice, appreciate your reply @Andrej Istomin
@Daniel Even if you cannot vote, you can accept this answer by clicking the checkmark.
@AndrejIstomin yes i accepted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.