1

Doing some tutorials on java this simple foreach loop on a list is giving me an error in the foreach loop: "Unknown class: familyMembers". I dont see where the problem is. but i know is something simple. Thanks in advance.

Update: I have all the imports necessary.

    List<String> familyMembers = new ArrayList<String>();

    familyMembers.add("john");
    familyMembers.add("suzy");
    familyMembers.add("smith");

    for (String name : familyMembers){
        System.out.println(name);
    }
5
  • It works for me, without any syntax or runtime errors. Commented Nov 13, 2016 at 13:47
  • Wich java version are you using and compiling? I am assuming you have imported java.util.List and java.util.ArrayList; Commented Nov 13, 2016 at 13:47
  • where is the for loop implemented? it looks like familyMembers is a local method declareated variable Commented Nov 13, 2016 at 13:48
  • 3
    Add your full class code and exception here Commented Nov 13, 2016 at 13:48
  • Sorry guys I just noticed That i had the code placed outside the function just in the class. lol I worked overnight and am learning java instead of going to sleep. Thanks for your quick replies. Commented Nov 13, 2016 at 13:52

1 Answer 1

3

it looks like familyMembers is a method-locally declared variable

the for loop must be implemented in a method where the familyMembers can be access or scope

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

Comments

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.