0

I have the following code

set1.forEach( k -> {
   for (String s : set2) {
        if(s.split(";")[0].equals(k){
            //do something
        }
    }
...

but I have this error

k cannot be resolved to a variable

Is there a way to read this variable?

Thanks

3
  • 3
    Lambda expressions were introduced in JDK 8, probably you are using an earlier version. Commented Dec 16, 2016 at 12:07
  • 1
    WFM: ideone.com/LT5MKR can you post a minimal reproducible example? Commented Dec 16, 2016 at 12:07
  • @Adit I'm currently using JDK 8 Commented Dec 16, 2016 at 12:10

1 Answer 1

2

You missed one paranthesis after k. You should have two closed paranthesis as below near equals

    Set<String> set1 = new HashSet<>();

    Set<String> set2 = new HashSet<>();

    set1.forEach((k)-> {
        for (String string : set2) {
            if(string.split(":")[0].equals(k)){
                //do something
            }

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

2 Comments

Solved. if statement was not closed. The error was about variable k, but the problem was a missing parenthesis
@Fab Yes, you missed paranthesis of if

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.