Results 1 to 7 of 7

Thread: Help with lists

  1. #1
    datreta is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default 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.

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

    Default

    A few tips: read the API documentation for String.split( ... ) method and all about the List< ... > interface.

    kind regards,

    Jos

  3. #3
    datreta is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    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;
    	}
    	
    	
    }

  4. #4
    datreta is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Good morning.

    Can someone help me with this one? It's due today... :o

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

    Default

    Quote Originally Posted by datreta View Post
    Good morning.

    Can someone help me with this one? It's due today... :o
    Did you give it a try? Does it compile? Does it run?

    kind regards,

    Jos

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    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.

  7. #7
    datreta is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    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:

    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)
    The code in list is now as follows:

    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

  1. Integers and Lists
    By TGH in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 09:49 AM
  2. Linked Lists
    By vendetta in forum New To Java
    Replies: 6
    Last Post: 01-26-2010, 08:23 AM
  3. Stacks, lists...
    By little_polarbear in forum New To Java
    Replies: 7
    Last Post: 08-02-2008, 01:59 PM
  4. comparision between two lists
    By suprabha in forum Advanced Java
    Replies: 14
    Last Post: 08-01-2008, 02:49 PM
  5. Tudu Lists 2.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-11-2007, 03:32 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
  •