Results 1 to 2 of 2
Thread: Searching a linkedlist
- 04-21-2011, 06:50 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
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
And when I try get the colour printed out I get an error. I'm not sure how to fix it.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"); } }
- 04-22-2011, 06:08 PM #2
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Need help with LinkedList.
By fsuarjun03 in forum New To JavaReplies: 0Last Post: 04-18-2011, 07:27 PM -
I need help in LinkedList ...
By Usman in forum Advanced JavaReplies: 3Last Post: 03-25-2011, 01:38 AM -
LinkedList
By [RaIdEn] in forum New To JavaReplies: 7Last Post: 10-13-2009, 12:59 AM -
LinkedList help
By jigglywiggly in forum New To JavaReplies: 6Last Post: 09-19-2009, 07:24 AM -
how to use LinkedList
By fred in forum Advanced JavaReplies: 1Last Post: 07-24-2007, 01:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks