Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: What's wrong with this code

  1. #1
    Junior Member
    Join Date
    May 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What's wrong with this code

    i wanna create my own list but in the main i wanna print added items such as paris london etc while using for each loop but loop doesnt run and the error is required: array or java.lang.Iterable what should i do ? ty
    public static void main(String[] args) {
    Scanner input= new Scanner(System.in);
    MyList<String> vo=new MyList<String>();
    vo.add("paris");
    vo.add("london");
    vo.add("usa");
    for(String city:vo) {
    System.out.println(vo);
    }
    public class MyList<T> {
    public void add(T values){

    }
    public void update(T values) {

    }

    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: What's wrong with this code

    The variable city is assigned the values from the list vo one by one as the for loop executes.
    for(String city:vo) {
       System.out.println(vo);   // should print city NOT vo
    }

    See the tutorial: https://docs.oracle.com/javase/tutor...bolts/for.html
    Look at the discussion of enhanced for near the bottom.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. what's wrong with my code
    By vmsumalatha2002 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 1st, 2014, 05:40 AM
  2. WHAT IS WRONG WITH MY CODE
    By YG N JYP FAM4EVA in forum Object Oriented Programming
    Replies: 1
    Last Post: February 26th, 2013, 05:14 PM
  3. What's wrong with my code?
    By lindmando in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2011, 11:13 PM
  4. what is wrong in this code
    By rk.kavuri in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 6th, 2011, 03:13 PM
  5. Wrong code
    By whattheeff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2011, 05:30 PM