Results 1 to 12 of 12
  1. #1
    Schooling is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Help with linked node manipulation -- Easy

    So I am having trouble getting my "kiss" method(which takes a node and removes it from "KissRing" and assigns it to "frontOfYard"(a different list of linked AffectionNodes). The problem I am having is when I run my test class on it and it tried to use the printSchoolYard method after calling the "kiss" method but it goes into debug mode because frontOfYard is null when I try to assign String tempName = frontOfYard.name. Can you guys help me figure out why this is happening?

    Java Code:
    import java.util.ArrayList;
    
    public class AffectionManager {
    	
    	private AffectionNode frontOfRing;
    	private AffectionNode frontOfYard;
    	
    	// Constructor for the AffectionManager class that creates a list of linked AffectionNodes.
    	public AffectionManager(ArrayList<String> names){
    			
    			String front = names.get(0);
    			frontOfRing = new AffectionNode(front);
    			AffectionNode current = frontOfRing;
    			for(int i = 1; i < names.size(); i++){
    				String currentName = names.get(i);
    
    				current.next = new AffectionNode(currentName);
    				current = current.next;
    				
    			}
    	
    			
    	}
    	
    	// Prints out a list of the current kiss ring with who is "so into" who.
    	public void printKissRing(){
    		String tempName = frontOfRing.name;
    		AffectionNode current = frontOfRing;
    		while(current.next != null){
    			String name = current.name;
    			String nextName = current.next.name;
    			System.out.println("  " + name + " is so into " + nextName);
    			current = current.next;
    		}
    		System.out.println("  " + current.name +" is so into " + tempName);
    	
    	
    	}
    	
    	// Prints out a list of the current Schoolyard with who was kissed by who.
    	public void printSchoolyard(){
    		String tempName = frontOfYard.name;
    		AffectionNode current = frontOfYard;
    		while(current.next != null){
    			tempName = current.name;
    			String nextName = current.next.name;
    			System.out.println("  " + tempName + " was kissed by " + nextName);
    			current = current.next;
    		}
    		System.out.println(current.name +" was kissed by " + tempName);
    	
    	
    	}
    	
    	// Returns true if the string passed matches one of the names within the KissRing, otherwise returns false.
    	public boolean kissRingContains(String name){
    		AffectionNode temp = frontOfRing;
    		String tempName;
    		while(temp != null){
    			tempName = temp.name;
    			if(name == tempName){
    				return true;
    			}
    			temp = temp.next;                  
    		}
    		return false;
    	}
    	
    	// Returns true if the string passed matches one of the names within the Schoolyard, otherwise returns false.
    	public boolean schoolyardContains(String name){
    		AffectionNode temp = frontOfYard;
    		String tempName;
    		while(temp != null){
    			tempName = temp.name;
    			if(name == tempName){
    				return true;
    			}
    			temp = temp.next;
    		}
    		return false;
    	}
    	
    	// Returns true if there is only 1 AffectionNode left in the KissRing, otherwise returns false.
    	public boolean isGameOver(){
    		if(frontOfRing.next == null){
    			return true;
    		}
    		return false;
    	}
    	
    	// Returns the string of the winner if game is over, otherwise returns null.
    	public String winner(){
    		if(frontOfRing.next == null){
    			return frontOfRing.name;
    		}
    		return null;
    	}
    	
    	// Takes the string passed in and removes any name in the KissRing that matches and adds to the front of the Schoolyard.
    	public void kiss(String name){
    		AffectionNode current = frontOfRing;
    		AffectionNode temp = frontOfYard;
    		
    		while(current.next != null){
    			
    			String currentName = current.next.name;
    			if(name.equals(currentName) && current.next.next != null){
    				frontOfYard = new AffectionNode(currentName, temp);
    				
    				current.next = current.next.next;
    				
    			} else {
    				current = current.next;
    			}
    			
    			
    		}
    	}
    	
    	// Returns a string representation of an AffectionManager object.
    	public String toString(){
    		System.out.println("[");
    		String s = " ";
    		AffectionNode current = frontOfRing;
    		while(current != null){
    			s = s + current.name;
    			current = current.next;
    		}
    		return s;
    	}
    
    }

  2. #2
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Help with linked node manipulation -- Easy

    Do you call kiss() before printSchoolyard()?
    There is nobody in the schoolyard before kiss() is called.

  3. #3
    Schooling is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: Help with linked node manipulation -- Easy

    Quote Originally Posted by brynpttrsn View Post
    Do you call kiss() before printSchoolyard()?
    There is nobody in the schoolyard before kiss() is called.
    Well, yes I see your point. But I call printSchoolyard() then call kiss() then call printSchoolyard().

    Test class:
    Java Code:
    public class AffectionManagerTest {
    	public static void main(String[] args){
    		ArrayList<String> names = new ArrayList<String>();
    		names.add("Jim");
    		names.add("Sally");
    		names.add("Duncan");
    		names.add("Thad");
    		names.add("Patty");
    		
    		AffectionManager affection = new AffectionManager(names);
    		
    		System.out.println(affection.toString());
    		affection.printKissRing();
    		
    		System.out.println();
    		System.out.println();
    		
    		affection.kiss("Jim");
    		affection.printKissRing();
    		
    		System.out.println();
    		affection.printSchoolyard();
    	}
    }

  4. #4
    Schooling is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: Help with linked node manipulation -- Easy

    Okay figured out the problem while testing. The kiss method works fine unless it is on the first iteration on the loop. It has to do with the frontOfYard being null but I am not sure why it is happening. Here is kiss() method:

    Java Code:
    public void kiss(String name){
    		AffectionNode current = frontOfRing;
    		AffectionNode temp = frontOfYard;
    		
    		while(current.next != null){
    			
    			String currentName = current.next.name;
    			if(name.equals(currentName) && current.next.next != null){
    				frontOfYard = new AffectionNode(currentName, temp);
    				
    				current.next = current.next.next;
    				
    			} else {
    				current = current.next;
    			}

  5. #5
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Help with linked node manipulation -- Easy

    [s]What exactly is the if statement in the loop supposed to do? the code inside isn't being executed.[/s]
    Ok I figured that out. I would suggest looking back at where you are assigning currentName in your while loop.
    Last edited by brynpttrsn; 05-06-2012 at 11:22 PM.

  6. #6
    Schooling is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: Help with linked node manipulation -- Easy

    Quote Originally Posted by brynpttrsn View Post
    What exactly is the if statement in the loop supposed to do? the code inside isn't being executed.
    It is supposed to change the reference(so that the element is being removed from frontOfYard and then add that element into frontOfYard). And it works unless it is the first iteration of the loop. If I pass the second name it executes properly.

  7. #7
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Help with linked node manipulation -- Easy

    Quote Originally Posted by brynpttrsn View Post
    Ok I figured that out. I would suggest looking back at where you are assigning currentName in your while loop.
    Response Ninjad.

  8. #8
    Schooling is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: Help with linked node manipulation -- Easy

    Quote Originally Posted by brynpttrsn View Post
    Response Ninjad.
    So you are saying it should be current.name? because I dont think it will work set up that way. When I tried it there were multiple names missing from the schoolYard when only one should have been missing, and that name was still in the KissRing instead.

  9. #9
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Help with linked node manipulation -- Easy

    Ok I get whats going on. While referencing current.next you can change that without problem and it will affect the nodes next element. You need something that changes the frontOfRing node if the front is referenced.

  10. #10
    Schooling is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: Help with linked node manipulation -- Easy

    Quote Originally Posted by brynpttrsn View Post
    Ok I get whats going on. While referencing current.next you can change that without problem and it will affect the nodes next element. You need something that changes the frontOfRing node if the front is referenced.
    Alright well I edited:
    Java Code:
    	public void kiss(String name){
    		AffectionNode current = frontOfRing;
    		AffectionNode temp = frontOfYard;
    		
    		while(current.next != null){
    			String currentName = "";
    			if(name.equals(frontOfRing.name)){
    			currentName = current.name;
    			
    			} else {
    			currentName = current.next.name;
    			}
    			if(name.equals(currentName) && current.next.next != null){
    				frontOfYard = new AffectionNode(currentName, temp);
    				
    				current.next = current.next.next;
    				
    			} else {
    				current = current.next;
    			}
    			
    			
    		}
    	}
    However it is still giving me the wrong list when printed out.
    This is how it starts:

    Jim is so into Sally
    Sally is so into Duncan
    Duncan is so into Thad
    Thad is so into Patty
    Patty is so into Jim

    After Kiss it should be:

    Sally is so into Duncan
    Duncan is so into Thad
    Thad is so into Patty
    Patty is so into Sally

    However it prints out now:
    Jim is so into Patty
    Patty is so into Jim

    and that is all.. Lol

  11. #11
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Help with linked node manipulation -- Easy

    If the name equals the frontOfRing node, you should be changing the frontOfRing to be the next node in the list.

  12. #12
    Schooling is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: Help with linked node manipulation -- Easy

    Quote Originally Posted by brynpttrsn View Post
    If the name equals the frontOfRing node, you should be changing the frontOfRing to be the next node in the list.
    Ahhhh got it completely functional with a few tweaks. Thanks bryn for helpin me out its appreciated :D

Similar Threads

  1. Linked node assignment help
    By Schooling in forum New To Java
    Replies: 4
    Last Post: 05-06-2012, 12:54 AM
  2. Replies: 3
    Last Post: 05-05-2011, 04:33 PM
  3. Creating a node for polynomial linked list
    By Java-Guy in forum New To Java
    Replies: 8
    Last Post: 03-17-2011, 09:27 PM
  4. Linked List node class help!!
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 05-03-2010, 02:21 AM
  5. Linked List Manipulation
    By chrisdb89 in forum New To Java
    Replies: 3
    Last Post: 11-21-2008, 07:07 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
  •