Results 1 to 20 of 20
  1. #1
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default List Java Help *Please*

    Hey Guys,

    This is my assignment:
    1. Rename SLListPartial to SLList and complete it by writing code for the methods that are stubs. Do NOT use private methods addFirst, addAfter, getNode shown in the text

    2. Write a SLLIstIter inner class for SLList. This class should implement Iterator, not ListIterator. Write methods hasNext, next, and remove for this class.

    Test your code by

    * reading the tokens in the file data.txt and adding them to an SLList of String
    * traversing the list printing its content
    * Removing from the list all tokens of length greater than 5
    * traversing again the list printing its content


    This my code that i have done:
    Java Code:
    package Lab_4;
    
    import java.util.*;
    import java.io.*;
    import java.io.File;
    
    /**
     *
     * @author Owner
     */
    public class SLList<E> extends AbstractList<E> implements List<E>, Iterable<E> {
    
        /** data fields */
        private Node<E> head = null;
        private int size = 0;
    
        public static void main(String[] args) throws IOException {
            List<String> list = new SLList<String>();
            Scanner file = new Scanner(new File("data.txt"));
    
            while (file.hasNext()) {
                list.add(file.next());
            }
            System.out.println(list);
            Iterator<String> it = list.iterator();
    
            while (it.hasNext()) {
                String next = it.next();
    
                if (next.length() > 5) {
                    it.remove();
                }
            }
    
    
    
    
            System.out.println(list);
        }
    
        private static class Node<E> {
    
            private E data;
            private Node<E> next;
    
            private Node(E data, Node<E> node) {
                this.data = data;
                this.next = node;
            }
    
            private Node(E data) {
                this(data, (Node<E>) null);
            }
        }
    
        @Override
        public String toString() {
            String output = "[";
    
            Node<E> curr = head;
    
            while (curr != null) {
                output += curr.data + ", ";
                curr = curr.next;
            }
    
            output = output.substring(0, output.length() - 2) + "]";
    
            return output;
        }
    
        @Override
        public int size() {
            return size;
        }
    
        @Override
        public int indexOf(Object obj) {
            /** do this one. Hint: To compare obj with the item referenced by
             * temp, use the following code:
             * if (temp.data.getClass() == obj.getClass() &&
            temp.data.equals( (E) obj)) {
            return cursor;
             */
            Node<E> curr = head;
    
            int idx = 0;
    
            while (curr != null) {
                if (curr.data.getClass() == obj.getClass() && curr.data.equals((E) obj)) {
                    return idx;
                }
                curr = curr.next;
                idx++;
            }
    
    
            return -1;
        }
    
        @Override
        public void add(int index, E item) {
    
            // This is the following hints i have recieved but doesnt help me
            // Do this without using getNode, addFirst, or addLast
            /** algorithm
            1. if index is not valid (too small or too big) throw an exception
            2. Set temp to head and i to 0
            3. while i is less than index-1
            Advance temp down the list and increment i
            4. if index is 0
            insert at list head so head points to a new node
            else
            Insert a new node between temp and temp's successor
            5. Increment size
             */
            // size check
            if (index < 0 || index > size) {
                throw new IndexOutOfBoundsException("Index: " + index);
            }
    
            // case index = 0
            if (index == 0) {
                head = new Node<E>(item, head);
            } // case index > 0
            else {
    
                Node<E> curr = head;
    
                while (index > 1) // searching for node before location
                {
                    index--;
                    curr = curr.next;
                }
    
                curr.next = new Node<E>(item, curr.next);
            }
    
            // update size
            size++;
        }
    
        @Override
        public boolean add(E item) {
            add(size, item);
            return true;
        }
    
        @Override
        public E get(int index) {
            Node<E> curr = head;
    
            while (index > 0 && curr != null) {
                index--;
                curr = curr.next;
            }
    
            if (curr == null) {
                return null;
            } else {
                return curr.data;
            }
        }
    
        @Override
        public Iterator<E> iterator() {
            return new SLListIter<E>(this);
        }
    
        private class SLListIter<E> implements Iterator<E> {
    
            private Node<E> prevItem = null; // prev item is node before last item returned for remove()
            private Node<E> nextItem = null;
            private Node<E> lastItemReturned = null;
            // link to list to be able to remove head
            private SLList<E> list;
    
            public SLListIter(SLList<E> list) {
                // I need to fill this in
                nextItem = list.head;
                this.list = list;
                prevItem = list.head;
            }
    
            @Override
            public boolean hasNext() {
                return nextItem != null;
            }
    
            @Override
            public E next() {
                if (nextItem == null) {
                    return null;
                } else {
    
                    lastItemReturned = nextItem;
                    nextItem = nextItem.next;
    
                    // prev item must stay 2 behind next item
                    if (nextItem != list.head && lastItemReturned != list.head) {
                        prevItem = prevItem.next;
                    }
    
                    return nextItem.data;
    
                }
            }
    
            @Override
            public void remove() {
    
                // if last item was the head, remove it
                if (lastItemReturned == list.head) {
                    list.head = list.head.next;
                    prevItem = list.head;
                } // otherwise, do normal remove
                else {
                    prevItem.next = nextItem;
                }
                lastItemReturned.next = null;
    
                // update size
                list.size--;
            }
        }
    }

    This is the error i get:
    run:
    Exception in thread "main" java.io.FileNotFoundException: data.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
    at java.util.Scanner.<init>(Scanner.java:636)
    at Lab_4.SLList.main(SLList.java:19)
    Java Result: 1


    Can Someone please help me

    Thanks alot,

  2. #2
    Join Date
    Sep 2010
    Posts
    12
    Rep Power
    0

    Default

    in this issue you can see this message (The system cannot find the file specified), it means the file can not find via program then you have to use absolute path in this case, for example stead of data.txt you have to use D:\foo\bar\data.txt

  3. #3
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    I tried and still dont work

  4. #4
    Join Date
    Sep 2010
    Posts
    12
    Rep Power
    0

    Default

    if same you get older message, you have to use forward slash (/) stead of back slash (\) in path, it's better that you use a IDE like eclipse.

  5. #5
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    Okay i got it to read the file, but NOW i am getting this error:

    Exception in thread "main" java.lang.NullPointerException
    [joe, josephine, samuel, sue, ann, sid, cameron, daniel, john]
    at Lab_4.SLList$Node.access$102(SLList.java:42)
    at Lab_4.SLList$SLListIter.remove(SLList.java:219)
    at Lab_4.SLList.main(SLList.java:32)

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Which line is line 42?

  7. #7
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    private static class Node<E> {

    private E data;
    private Node<E> next;

    private Node(E data, Node<E> node) {
    this.data = data;
    this.next = node;
    }

    private Node(E data) {
    this(data, (Node<E>) null);
    }
    }

    Its that part^^^

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Try debugging your code by adding some print outs where there can be undetected null variables. For example add these at the appropriate points:

    System.out.println("next=" + next);

    System.out.println("nextItem=" + nextItem);

  9. #9
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    What do those codes do
    and what do you mean by appropriate spots?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    appropriate spots
    Just before the variable in the print out is used, where it could cause a NPE.

    You have holes in your logic where you need to see if variables are null.
    Printing them out will show you where

  11. #11
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    Does it go right here:

    Java Code:
    package Lab_4;
    
    import java.util.*;
    import java.io.*;
    import java.io.File;
    
    /**
     *
     * @author Owner
     */
    public class SLList<E> extends AbstractList<E> implements List<E>, Iterable<E> {
    
        /** data fields */
        private Node<E> head = null;
        private int size = 0;
    
        public static void main(String[] args) throws IOException {
            List<String> list = new SLList<String>();
            Scanner file = new Scanner(new File("C:\\Users\\Akash Patel\\Desktop\\data.txt"));
            System.out.println("next=" + next);
    
    System.out.println("nextItem=" + nextItem); 
    
            while (file.hasNext()) {
                list.add(file.next());
            }
            System.out.println(list);
            Iterator<String> it = list.iterator();
    
            while (it.hasNext()) {
                String next = it.next();
    
                if (next.length() > 5) {
                    it.remove();
                }
            }
    
    
    
    
            System.out.println(list);
        }
    
        private static class Node<E> {
    
            private E data;
            private Node<E> next;
    
            private Node(E data, Node<E> node) {
                this.data = data;
                this.next = node;
            }
    
            private Node(E data) {
                this(data, (Node<E>) null);
            }
        }
    
        @Override
        public String toString() {
            String output = "[";
    
            Node<E> curr = head;
    
            while (curr != null) {
                output += curr.data + ", ";
                curr = curr.next;
            }
    
            output = output.substring(0, output.length() - 2) + "]";
    
            return output;
        }
    
        @Override
        public int size() {
            return size;
        }
    
        @Override
        public int indexOf(Object obj) {
            /** do this one. Hint: To compare obj with the item referenced by
             * temp, use the following code:
             * if (temp.data.getClass() == obj.getClass() &&
            temp.data.equals( (E) obj)) {
            return cursor;
             */
            Node<E> curr = head;
    
            int idx = 0;
    
            while (curr != null) {
                if (curr.data.getClass() == obj.getClass() && curr.data.equals((E) obj)) {
                    return idx;
                }
                curr = curr.next;
                idx++;
            }
    
    
            return -1;
        }
    
        @Override
        public void add(int index, E item) {
    
            // This is the following hints i have recieved but doesnt help me
            // Do this without using getNode, addFirst, or addLast
            /** algorithm
            1. if index is not valid (too small or too big) throw an exception
            2. Set temp to head and i to 0
            3. while i is less than index-1
            Advance temp down the list and increment i
            4. if index is 0
            insert at list head so head points to a new node
            else
            Insert a new node between temp and temp's successor
            5. Increment size
             */
            // size check
            if (index < 0 || index > size) {
                throw new IndexOutOfBoundsException("Index: " + index);
            }
    
            // case index = 0
            if (index == 0) {
                head = new Node<E>(item, head);
            } // case index > 0
            else {
    
                Node<E> curr = head;
    
                while (index > 1) // searching for node before location
                {
                    index--;
                    curr = curr.next;
                }
    
                curr.next = new Node<E>(item, curr.next);
            }
    
            // update size
            size++;
        }
    
        @Override
        public boolean add(E item) {
            add(size, item);
            return true;
        }
    
        @Override
        public E get(int index) {
            Node<E> curr = head;
    
            while (index > 0 && curr != null) {
                index--;
                curr = curr.next;
            }
    
            if (curr == null) {
                return null;
            } else {
                return curr.data;
            }
        }
    
        @Override
        public Iterator<E> iterator() {
            return new SLListIter<E>(this);
        }
    
        private class SLListIter<E> implements Iterator<E> {
    
            private Node<E> prevItem = null; // prev item is node before last item returned for remove()
            private Node<E> nextItem = null;
            private Node<E> lastItemReturned = null;
            // link to list to be able to remove head
            private SLList<E> list;
    
            public SLListIter(SLList<E> list) {
                // I need to fill this in
                nextItem = list.head;
                this.list = list;
                prevItem = list.head;
            }
    
            @Override
            public boolean hasNext() {
                return nextItem != null;
            }
    
            @Override
            public E next() {
                if (nextItem == null) {
                    return null;
                } else {
    
                    lastItemReturned = nextItem;
                    nextItem = nextItem.next;
    
                    // prev item must stay 2 behind next item
                    if (nextItem != list.head && lastItemReturned != list.head) {
                        prevItem = prevItem.next;
                    }
    
                    return nextItem.data;
    
                }
            }
    
            @Override
            public void remove() {
    
                // if last item was the head, remove it
                if (lastItemReturned == list.head) {
                    list.head = list.head.next;
                    prevItem = list.head;
                } // otherwise, do normal remove
                else {
                    prevItem.next = nextItem;
                }
                lastItemReturned.next = null;
    
                // update size
                list.size--;
            }
        }
    }

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Does the code compile the way you show it? Are the variables defined there?

    The two print out statements go in different locations.
    Put each one just before where the variable being printed is referenced or just after where the variable is being changed.
    There is more than one place where they should be placed.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For anyone considering helping, you may wish to see the cross-post here: Need Help (Java) Please - Dev Shed

    and his previous interesting thread in this forum on the subject: SLLIstIter inner Help

    For the OP, please notify us of all cross-posts.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Another idea for having others help you in debugging your code. Replace the Scanner reading an external file with an internal String:

    String Data = "data1\ndata2\ndata3\nlastData\n";
    Scanner file = new Scanner(Data); //new File("data.txt"));

  15. #15
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    I get the same error by doing that string.
    any solution..its due in 1 hour

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Yes that was not to solve the problem. It was a way to allow others to easily work on your problem without needing external files.

    Have you tried doing the debugging using the print outs as I suggested earlier?
    The print outs will show you where you have null variables so you can fix you code to handle the null values.

  17. #17
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    Yah i tried debugging but i says it cannot compile all of it.

    Doesnt make sense, it prints out my data, but still got an error somewhere

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    it prints out my data, but still got an error somewhere
    The print outs don't fix the problem, you do.
    Add more print outs especially around where the NPE was.

    Here is the print out with the print outs I added to your code:

    list1=[data1, data2, data3, lastData]
    nextItem=data=data2, next=data=data3, next=data=lastData, next=null
    next=data2
    next.length()= 5
    nextItem=data=data3, next=data=lastData, next=null
    next=data3
    next.length()= 5
    nextItem=data=lastData, next=null
    next=lastData
    next.length()= 8
    nextItem=null
    next=null
    list2=[data1, data2, data3]
    Last edited by Norm; 09-28-2010 at 02:39 AM.

  19. #19
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Quote Originally Posted by acash229 View Post
    private static class Node<E> {

    private E data;
    private Node<E> next;

    private Node(E data, Node<E> node) {
    this.data = data;
    this.next = node;
    }

    private Node(E data) {
    this(data, (Node<E>) null);
    }
    }

    Its that part^^^
    What?
    All of that is line 42?
    I find that slightly hard to believe...:)

  20. #20
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,349
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    What?
    All of that is line 42?
    I find that slightly hard to believe...:)
    Yep, some extremely long lines wrap like that; if the parts of the line are properly spaced out they look like the above ;-)

    kind regards,

    Jos

Similar Threads

  1. Linked List in java
    By zaydz in forum New To Java
    Replies: 14
    Last Post: 06-28-2010, 12:53 PM
  2. Replies: 2
    Last Post: 11-22-2009, 05:24 PM
  3. Replies: 3
    Last Post: 11-21-2009, 05:48 PM
  4. how to create list of list in java ???
    By ilayaraja in forum Advanced Java
    Replies: 1
    Last Post: 10-26-2009, 04:30 PM
  5. How to Search a List in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:38 PM

Posting Permissions

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