Results 1 to 2 of 2
  1. #1
    Howlin is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Searching a linkedlist

    Hello
    I have a program and you put in peoples names and their favourite colours. At the moment I'm trying to get it so I ask whos favourite colour would you like to know and it would print out X's favourite colour is X. I am having a problem at trying to retrieve the favourite colour.
    I have
    PHP Code:
    package list;
    
    // A menu-driven friend accumulation and listing program
    
    import java.util.*;  // Java library for linked lists referenced here
    
    class Friend
    {
    	String name, colour;
    
    	//A method to allow a new friends name and favourite colour to be added
    	public Friend(String friendsName, String favouriteColour)
    	{
    		name = friendsName;
    		colour = favouriteColour;
    	}
    
    	// method to respond to a message sent
    	// to a rectangle to set its length
    	public void setName(String nameValue)
    	{
    		name = nameValue;
    	}
    
    	// method to respond to a message sent
    	// to a rectangle to set its breadth
    	public void setColour(String colourValue)
    	{
    		colour = colourValue;
    	}
    
    
    	// method to allow a Friend to respond
    	// to a System.out.println
    	public String toString()
    	{
    		return("\n" + name + "\t\t\t" + " " + colour);
    	}
    
    
    	// method to allow a Friend object to say
    	// whether it's equal to another rectangle or
    	// not
    	public boolean equals(Friend rIn)
    	{
    		return name.equals(rIn.name) && colour.equals(rIn.colour);
    	}
    	
    	public String getName()
    	{
    		return name;
    	}
    	public String getColour()
    	{
    		return colour;
    	}
    }
    
    
    public class People
    {
    	public static void main(String [] args)
    	{
    		Scanner scanner = new Scanner(System.in);
    
    		//A LinkedList for storage of friends names and favourite colour
    		List<Friend> friends = new LinkedList<Friend>();
    		
    		ListIterator<Friend> iterator; 
    		String friendName, faveColour, friendsName;
    		int choice;
    
    		do
    		{
    			System.out.println("\nPlease choose your menu choice");
    			System.out.println("Add a friend and their favourite colour \t\t 1");
    			System.out.println("See a list of your friends and their favourite colour \t 2");
    			System.out.println("Remove a friend \t\t\t\t\t 3");
    			System.out.println("Search for a named friend's favourite colour \t\t 4");
    			System.out.println("Quit \t\t\t\t\t\t\t 0");
    			choice = scanner.nextInt();
    
    			if(choice == 1) //Adding a friend and their favourite colour
    			{
    				// ask the user for the length and the breadth of the new Rectangle
    				System.out.println("\nPlease type in your friends first name and favourite colour");
    				System.out.println("First name");
    				friendName = scanner.next();
    				System.out.println("Favourite colour");
    				faveColour = scanner.next();
    				iterator = friends.listIterator();
    				
    				if(iterator.hasNext() && iterator.next().getName().equals(friendName))
    				{
    					System.out.println(friendName + " is already a friends name, please choose another name for your friend."); 
    				}
    				else 
    				{				
    				friends.add(new Friend(friendName, faveColour));
    				}
    				
    				
    			}
    			else if (choice == 2) //Seeing a list of all of your friends and their favourite colour
    			{
    				int i = friends.size();
    				if (i > 0)
    				{
    					//code for printing out the list of your friends and their favourite colours.
    					System.out.println("Friends name:\t\t Favourite colour: \n" + friends);
    				}
    				else
    				{
    					System.out.println("You don't have any friends to list!");
    				}
    			}
    			
    			else if (choice == 3) //Removing a friend and their favourite colour
    			{
    				friendsName = null;
    				int i = friends.size();
    				if (i == 0)
    				{
    					System.out.println("You don't have any friends to remove!");
    				}
    				else 
    				{
    					System.out.println("Who would you like to remove as a friend?");
    					friendsName = scanner.next();
    					
    					iterator = friends.listIterator();
    					if(iterator.hasNext() && !iterator.next().getName().equals(friendsName))
    					{
    					System.out.println(friendsName + " isn't in your list of friend(s).");
    					}
    					else
    					{
    					iterator.remove();
    					System.out.println(friendsName + " has been removed as a friend!");	
    					}
    					
    				}
    			}
    			
    			else if (choice == 4) //Searching for a friends favourite colour
    			{
    				int i = friends.size();
    				if (i == 0)
    				{
    					System.out.println("You don't have any friends to search!");
    				}
    				else 
    				{
    					friendsName = null;
    					faveColour = null;
    					System.out.println("Name the friend whos favourite colour you would like to know.");
    					friendsName = scanner.next();
    				
    					iterator = friends.listIterator();
    					if(iterator.hasNext() && !iterator.next().getName().equals(friendsName))
    					{
    						System.out.println(friendsName + " isn't a friend of yours.");
    					}
    					else
    					{
    						faveColour = iterator.next().getColour();
    						System.out.println(friendsName + "'s favourite colour is " + faveColour);
    					}
    				}
    			}
    		}
    		while(choice != 0);
    		System.out.println("\nThank you for using this program. Come back soon!\n\n");
    	}
    }
    And when I try get the colour printed out I get an error. I'm not sure how to fix it.

  2. #2
    Howlin is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Need help with LinkedList.
    By fsuarjun03 in forum New To Java
    Replies: 0
    Last Post: 04-18-2011, 07:27 PM
  2. I need help in LinkedList ...
    By Usman in forum Advanced Java
    Replies: 3
    Last Post: 03-25-2011, 01:38 AM
  3. LinkedList
    By [RaIdEn] in forum New To Java
    Replies: 7
    Last Post: 10-13-2009, 12:59 AM
  4. LinkedList help
    By jigglywiggly in forum New To Java
    Replies: 6
    Last Post: 09-19-2009, 07:24 AM
  5. how to use LinkedList
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 01:52 AM

Posting Permissions

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