Results 1 to 20 of 20
Thread: List Java Help *Please*
- 09-27-2010, 05:22 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
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,
- 09-27-2010, 06:56 AM #2
Member
- Join Date
- Sep 2010
- Posts
- 12
- Rep Power
- 0
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
- 09-27-2010, 08:01 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
I tried and still dont work
- 09-27-2010, 08:23 AM #4
Member
- Join Date
- Sep 2010
- Posts
- 12
- Rep Power
- 0
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.
- 09-27-2010, 10:01 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
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)
- 09-27-2010, 01:18 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Which line is line 42?
- 09-27-2010, 06:42 PM #7
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
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^^^
- 09-27-2010, 06:58 PM #8
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);
- 09-27-2010, 07:30 PM #9
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
What do those codes do
and what do you mean by appropriate spots?
- 09-27-2010, 07:47 PM #10
Just before the variable in the print out is used, where it could cause a NPE.appropriate spots
You have holes in your logic where you need to see if variables are null.
Printing them out will show you where
- 09-27-2010, 08:41 PM #11
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
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--; } } }
- 09-27-2010, 08:58 PM #12
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.
-
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.
- 09-27-2010, 09:21 PM #14
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"));
- 09-28-2010, 01:48 AM #15
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
I get the same error by doing that string.
any solution..its due in 1 hour
- 09-28-2010, 01:55 AM #16
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.
- 09-28-2010, 02:17 AM #17
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
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
- 09-28-2010, 02:28 AM #18
The print outs don't fix the problem, you do.it prints out my data, but still got an error somewhere
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.
- 09-28-2010, 08:37 AM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
- 09-28-2010, 08:59 AM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Linked List in java
By zaydz in forum New To JavaReplies: 14Last Post: 06-28-2010, 12:53 PM -
How do I list values of an arrays in a comma seperted list
By nmvictor in forum New To JavaReplies: 2Last Post: 11-22-2009, 05:24 PM -
How do I list values of an arrays in a comma seperted list
By nmvictor in forum New To JavaReplies: 3Last Post: 11-21-2009, 05:48 PM -
how to create list of list in java ???
By ilayaraja in forum Advanced JavaReplies: 1Last Post: 10-26-2009, 04:30 PM -
How to Search a List in Java
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks