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 3 of 3

Thread: Implementing Iterable?!! Unsolvable problem

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Implementing Iterable?!! Unsolvable problem

    So my teacher told us that we had to write a method called iterator() in a class called Dictionary. Dictionary has one private instance variable (and may only have that one instance variable) that is an ArrayList of strings. Dictionary implements Iterable<E>. I made my class name Dictionary<E> because I was under the impression that that was the only way to implement Iterable<E>. My problem is that the method iterator is supposed to provide a java.util.Iterator that iterates over the Dictionary in order, a word at a time, from beginning to end. First of all what the hell does that mean? Second of all when I tried to use ArrayList's method iterator to return an iterator of my arrayList, it won't let me because my teacher told me my instance variable arraylist had to be of strings, and that my iterator method had to return a java.util.iterator<String>. However, my iterator method will only return an iterator of type E, and so I get an Error stating:
    found : java.util.Iterator<java.lang.String>
    required: java.util.Iterator<E>

    Here's my code, please try to help I am at a point of staring at code.
    import java.util.ArrayList;
    import java.util.Scanner;
    public class Dictionary<E> implements Iterable<E>
    {
       private ArrayList<String> list;
       public Dictionary()
       {}
       public Dictionary(String fileName) 
       {
          Scanner sc = new Scanner(fileName);
          while(sc.hasNext() == true)
          {
             list.add(sc.next());
          }   
       }
       public java.util.Iterator<String> iterator()
       {
           return list.iterator();
       }       
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Implementing Iterable?!! Unsolvable problem

    First thing's first, you never initialized list, so you'll get a null pointer even if it did run properly. Initialize it before you call the list.add(...) method.

    Second, E is something called a: generic. You can substitute it for any Object, and you don't need to include <E> after your class name, just after the Iterable implementation. All of that simply means to change this:
    public class Dictionary<E> implements Iterable<E>
    to this:
    public class Dictionary implements Iterable<String>

    That should be syntactically correct and do what you want.
    Last edited by aussiemcgr; May 3rd, 2012 at 10:18 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Implementing Iterable?!! Unsolvable problem

    I love you

    Quote Originally Posted by aussiemcgr View Post
    First thing's first, you never initialized list, so you'll get a null pointer even if it did run properly. Initialize it before you call the list.add(...) method.

    Second, E is something called a: generic. You can substitute it for any Object, and you don't need to include <E> after your class name, just after the Iterable implementation. All of that simply means to change this:
    public class Dictionary<E> implements Iterable<E>
    to this:
    public class Dictionary implements Iterable<String>

    That should be syntactically correct and do what you want.

Similar Threads

  1. Replies: 5
    Last Post: September 17th, 2011, 09:05 PM
  2. Implementing RSS Feeds
    By madhu_sushmi in forum Java Networking
    Replies: 0
    Last Post: April 1st, 2010, 07:57 PM
  3. Implementing Semaphores
    By bananasplitkids in forum Threads
    Replies: 1
    Last Post: March 27th, 2010, 04:35 AM
  4. Help needed regarding implementing RSS feed using JSP
    By smsouvik_2008 in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: February 21st, 2010, 04:34 AM
  5. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 04:38 PM

Tags for this Thread