Results 1 to 7 of 7
Thread: Help with lists
- 10-28-2010, 05:13 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
Help with lists
Hello.
I'm new here and to programming in Java. I'm starting to have classes now and I need help with a project. What I'm asked to do is the following:
- The user is asked to write a sentence;
- From that sentence, each word needs to be placed in a node of a chained list (i don't know if this is the correct term);
- Then, I need a new list with the sentence from the list before but with the words in a reverse order (ex: I am noob -> noob am I).
Hope someone can help me because I don't even know where to start.
Thank you in advance.
Regards.
- 10-28-2010, 05:19 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
A few tips: read the API documentation for String.split( ... ) method and all about the List< ... > interface.
kind regards,
Jos
- 10-28-2010, 10:29 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
Ok, after some documentation checking, I've come up with this. Can someone take a look if I'm on the right track? How can I see if both lists are ok?
PHP Code:import javax.swing.JOptionPane; public class menu { public static int options() { int op; do { String a = "Menu" + "\n1 - write a sentence" + "\n2 - words in a reverse order" + "\n3 - create an ordered bidirectional list with words that are not repeated" + "\n4 - delete word" + "\n5 - delete word between x and y" + "\n6 - Move nodes" + "\n7 - Copy nodes" + "\n8 - print lists" + "\n0 - exit"; op = Integer.parseInt(JOptionPane.showInputDialog(a + "\nOption? (0 to 8)")); } while ((op < 0) || (op > 8)); return op; } public static void main(String[] args) { list A = new list(); list B = new list(); int option = opcoes(); while (option != 0) { String sentence; switch (option) { case 1: { do sentence = JOptionPane.showInputDialog("Input sentence: "); while (sentence.compareTo("") == 0); A.receivesentence(sentence); break; } case 2: { do sentence = JOptionPane.showInputDialog("Input sentence: "); while (sentence.compareTo("") == 0); B.receivesentence2(sentence); break; } case 3: { break; } } } } }PHP Code:import java.util.*; public class list { private node head; public void receivesentence(String sentence){ String [] words = sentence.split(" "); for (int i = 0; i < words.length-1; i++){ this.insertordered(words[i]); } } public void receivesentence2(String sentence){ String [] words = sentence.split(" "); for (int i = 0; i < words.length-1; i++){ this.insertinverted(words[i]); } } public void insertordered(String word){ node new=new node(word); node temp=head; while((temp.getnext()!=null)&&(temp.getnext().getword().compareTo(new.getword())<0)){ temp=temp.getnext(); } new.setnext(temp.getnext()); new.setprev(temp); temp.setnext(new); } public void insertinverted(String word){ node new=new node(word); node temp=head; while((temp.getnext()!=null)&&(temp.getnext().getword().compareTo(new.getword())>0)){ temp=temp.getnext(); } new.setnext(temp.getnext()); new.setprev(temp); temp.setnext(new); } public String print() { String st = ""; if (head.getnext() != head) { node temp = head.getnext(); while(temp != head) { st += temp.getword() + " "; temp = temp.getnext(); } } else st += "The list is empty!"; return st; } }PHP Code:public class node { private String word; private node next; private node prev; public node(String word){ this.word=word; } public String getword() { return word; } public void setword(String word) { this.word = word; } public node getnext() { return next; } public void setnext(node next) { this.next = next; } public node getprev() { return prev; } public void setprev(node prev) { this.prev = prev; } }
- 10-29-2010, 09:51 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
Good morning.
Can someone help me with this one? It's due today... :o
- 10-29-2010, 09:54 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 10-29-2010, 10:37 AM #6
some points i found while i try to compile the code:
- since new is a java keyword you can't use it as an identifier. change this identifier.
- when you call the method insertordered the variable temp is still null, so you get an NullPointerException. adapt the code to verify if temp is null.
i can make some semantic checks when the code is compilable.
- 10-29-2010, 11:33 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
Thank you for your answers.
I changed the code like you said (I think) and it does compile. However, when I type a sentence, eclipse gives me the following errors:
The code in list is now as follows:PHP Code:Exception in thread "main" java.lang.NullPointerException at pack.list.insertordered(list.java:39) at pack.list.receivesentence(list.java:13) at pack.menu.main(menu.java:36)
PHP Code:package pack; import java.util.*; public class list { private node head; public void receivesentence(String sentence){ String [] words = sentence.split(" "); for (int i = 0; i < words.length-1; i++){ this.insertordered(words[i]); } } public void receivesentence2(String sentence){ String [] words = sentence.split(" "); for (int i = 0; i < words.length-1; i++){ this.insertinverted(words[i]); } } public void insertordered(String word){ node newnode=new node(word); node temp=head; if (temp == null){ while((temp.getnext()!=null)&&(temp.getnext().getword().compareTo(newnode.getword())<0)){ temp=temp.getnext(); } } newnode.setnext(temp.getnext()); newnode.setprev(temp); temp.setnext(newnode); } public void insertinverted(String word){ node newnode=new node(word); node temp=head; while((temp.getnext()!=null)&&(temp.getnext().getword().compareTo(newnode.getword())>0)){ temp=temp.getnext(); } newnode.setnext(temp.getnext()); newnode.setprev(temp); temp.setnext(newnode); } public String print() { String st = ""; if (head.getnext() != head) { node temp = head.getnext(); while(temp != head) { st += temp.getword() + " "; temp = temp.getnext(); } } else st += "The list is empty!"; return st; } }
Similar Threads
-
Integers and Lists
By TGH in forum New To JavaReplies: 8Last Post: 01-27-2010, 09:49 AM -
Linked Lists
By vendetta in forum New To JavaReplies: 6Last Post: 01-26-2010, 08:23 AM -
Stacks, lists...
By little_polarbear in forum New To JavaReplies: 7Last Post: 08-02-2008, 01:59 PM -
comparision between two lists
By suprabha in forum Advanced JavaReplies: 14Last Post: 08-01-2008, 02:49 PM -
Tudu Lists 2.0
By JavaBean in forum Java SoftwareReplies: 0Last Post: 07-11-2007, 03:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks