Results 1 to 12 of 12
Like Tree2Likes
  • 2 Post By jim829

Thread: LinkedList iterable error assistance.

  1. #1
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default LinkedList iterable error assistance.

    I am doing this LinkedList implementiation that implements iterable<T>...
    However I am having trouble in my " public LinkedList(Iterable<T> iterable)" case. I tried to implemented but I get an error, and I wasn't able to fix it. Any help would be appreciated.
    Java Code:
    import java.util.Iterator;
    import java.util.*;
    
    public class LinkedList<T> implements Iterable<T> {
    	
    	public LinkedList<T> head;
    	
    	public LinkedList(){
    		head = null;
    	}
    	//Help Here!...
    	 public LinkedList(Iterable<T> iterable)
    	 {
    		 return new LinkedList(next);    //Error is given here..
    	 } 
    	
    	@Override
        public LinkedList<T> next() {
    		
    		if (list.next != null){
    			return list.next;
    		}
            throw new NoSuchElementException("Random Error");
    	}
    		
    	public Iterator<T> iterator() {
    		return new LinkedListIterator(next);    
    	}	
    		
    	@Override
        public boolean hasNext() {
            if (list.next == null){
    			return false;
    		}
    		else{
    			return true;
    		}
    	}
    	
    	//Reverse
    	public LinkedList<T> reverse(LinkedList<T> list){
    		if(list == null) 
    			return null;
    		if(list.next == null) 
    			return list;
    		LinkedList<T> secondElem = list.next;
    		list.next = null;
    		LinkedList<T> reverseAll = reverse(secondElem);
    		secondElem.Next = list;
    	
    		return reverseAll;
    	}
    	
    	//toString 
    	public String toString(){
    		/* StringBuffer result = new StringBuffer();
    		for() */
    		return "";
    	}
    
    	@Override
    	 public void remove() {
            throw new UnsupportedOperationException();
    	 }
    
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: LinkedList iterable error assistance.

    I get an error,
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: LinkedList iterable error assistance.

    Wow. an error turned out to be 14 errors

    Work on them a couple at a time:
    unexpected return value <<<<<<<< Constructors do not return a value

    error: cannot find symbol
    The compiler can not find a definition for the variable shown in the error message.


    Note: When writing this long of a class, it will be easier to fix the errors if the source is compiled often and the few errors fixed before adding more code.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: LinkedList iterable error assistance.

    To augment Norm's suggestion, I would also forget about implementing Iterable<T>. I would first focus
    on getting your LinkedList class working. Then I would work on implementing the Iterator interface
    (preferrably in an inner class). Then once that is working, implement Iterable<T>.

    Implementing Iterable<T> is trivial once you get the other stuff working.

    Regards,
    Jim
    Norm and Eat_Whatever like this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: LinkedList iterable error assistance.

    Okay, sounds great, I'll try and see what happen's. Thanks guys.

  6. #6
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: LinkedList iterable error assistance.

    So I managed to reduce some errors, and leave iterable alone for now. But I am still getting the cannot find symbol error message on iterator part. I know that you said that the compiler can't find the definition of the variable, so can you please elaborate more on that? How would I go by fixing it. It's been driving me nuts


    And this is what I have so far:

    Java Code:
    import java.util.Iterator;
    import java.util.*;
    
    public class LinkedList<T> implements Iterable<T> {
    	
    	public LinkedList<T> head;
    	
    	public LinkedList(){
    		head = null;
    	}
    	
    	  public LinkedList(Iterable<T> iterable)
    	  {
    		//Will do later.  	  
    		  
    	  }
    		
    	@Override
    	 public Iterator<T> iterator() {
    		return new LinkedListIterator<T>(head);   //Error message here(cannot find symbol) 
    	}
    		
    	
    	//Reverse
    	public LinkedList<T> reverse(LinkedList<T> list){
    		if(list == null) 
    			return null;
    		if(list == null) 
    			return list;
    		LinkedList<T> secondE = list;
    		list = null;
    		LinkedList<T> reverseAll = reverse(secondE);
    		secondE = list;
    	
    		return reverseAll;
    	}
    	
    	//toString 
    	public String toString(){
    		/* 
    		String output = "";
    		if(head != null){
    			LinkedList<T> L = head.next;
    			while (L != null){
    				output += "[" + L.toString() + "]";
    				L = L.Next;	
    			}	
    		}
    		return output;
    		*/
    		return "";
    	}
    
    
    }

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: LinkedList iterable error assistance.

    getting the cannot find symbol error message
    Please copy the full text of the error message and paste it here. It has important info about the error.

    What is the symbol?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: LinkedList iterable error assistance.

    This is what it outputs:

    "LinkedList.java:20: error: cannot find symbol
    return new LinkedListIterator<T>(head);
    ^
    symbol: class LinkedListIterator
    location: class LinkedList<T>
    where T is a type-variable:
    T extends Object declared in class LinkedList
    1 error"

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: LinkedList iterable error assistance.

    Where is your LinkedListIterator class? And why are you passing a value to the constructor?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: LinkedList iterable error assistance.

    This is what I have so far, I was informed that next or hasnext() isn't required here, not to sure though.
    Java Code:
    private class LinkedListIterator implements Iterator<T>{
    		private LinkedList<T> next;
    		public LinkedListIterator()
    		{
    			next = head;
    		}
    		
    	}

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: LinkedList iterable error assistance.

    You were informed incorrectly. When you implement any interface, the methods must be provided (unless they have a default,
    which is new in Java 8 and something you need not worry about at this time). And it is hasNext() - (note camel case).


    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: LinkedList iterable error assistance.

    Oh ok, yeah they were saying it with regards to Java 8. But for now, I'll implement those methods and go from there.
    Thanks Jim.

Similar Threads

  1. New to Java and need assistance please with an error.
    By liv2dtumm2 in forum New To Java
    Replies: 7
    Last Post: 02-20-2014, 02:52 AM
  2. Replies: 1
    Last Post: 07-26-2013, 05:14 PM
  3. Compile error using LinkedList
    By dre in forum New To Java
    Replies: 7
    Last Post: 09-24-2011, 04:58 AM
  4. error message assistance
    By Sean_J in forum New To Java
    Replies: 12
    Last Post: 03-17-2010, 02:27 AM
  5. Error assistance
    By bobbychiken in forum New To Java
    Replies: 2
    Last Post: 11-21-2009, 10:54 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •