Results 1 to 4 of 4
- 09-08-2012, 02:51 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 9
- Rep Power
- 0
How to retrieve data from a linkedlist and display it?
I'm messing around with a program that I want to collect NFL games revenue. The user will input the stadium and and the game revenue. When done adding all the data they want they will enter any stadium and it will show total revenue.
Example Output...
Enter the data now.
On each line enter a stadium name and the game revenue
Enter done when you are finished
Giants 1000
Foxboro 500
Giants 1500
done
Enter the name of a stadium to get the total revenue for:
Giants
The total revenue is 2500.0
I'm trying to do something like that. My problem is that I don't know how to pull information from a Linked List from what the user types in. Say if they type giants , I not sure how to go into the linkedlist and display all the revenue for the giants.
Here is my code right now, and at the parts where it says //code in comments is where I'm stuck.
This is my code right now...[/B]Java Code:import java.util.LinkedList; import java.util.Scanner; public class LinkedLists { private final Scanner keyboard; private final LinkedList<String> stadiumNames; private final LinkedList<Integer> gameRevenue; public LinkedLists() { this.keyboard = new Scanner(System.in); this.stadiumNames = new LinkedList<String>(); this.gameRevenue = new LinkedList<Integer>(); } public void addData(String stadium, int revenue){ stadiumNames.add(stadium); gameRevenue.add(revenue); } public void loadDataFromUser() { System.out.println("On each line enter the stadium name and game revenue."); System.out.println("Enter done when you are finished."); boolean done = false; while(!done) { System.out.print("Enter thhe name of the stadium:"); String stadium = keyboard.next(); if (stadium.equals("done")) { done = true; } else { System.out.print("Enter game revenue: "); addData(stadium, keyboard.nextInt()); } } } // return -1 if not found public int getIndexForName(String name) { //code return -1; } public void showInfoForName(String name) { int index = getIndexForName(name); if (index==-1) { System.out.println("There is no stadium named " + name); } else { // code } } public void showInfoForName() { System.out.println("Enter thhe name of the stadium to get the total revenue for it."); showInfoForName(keyboard.next()); } public static void main(String[] args) { LinkedLists pgm = new LinkedLists(); pgm.loadDataFromUser(); pgm.showInfoForName(); } }
- 09-08-2012, 03:22 AM #2
Re: How to retrieve data from a linkedlist and display it?
Also posted at Stuck on a program - LinkedList - Dev Shed
Did you do what was suggested on the other site?If you don't understand my response, don't ignore it, ask a question.
- 09-08-2012, 03:27 AM #3
Member
- Join Date
- Jul 2012
- Posts
- 9
- Rep Power
- 0
Re: How to retrieve data from a linkedlist and display it?
Yeah I did that. I thought maybe I would use indexOf() but it's still confusing. Looking at a chart of methods isn't helping to much.
I'm looking for someone that can flat out explain how to do this to a beginner, looking at charts and guessing what to do for hours is just making it more frustrating.
- 09-08-2012, 03:38 AM #4
Re: How to retrieve data from a linkedlist and display it?
Often that is the ONLY way to get the answer.Looking at a chart of methods isn't helping to much.
What happens when you use the indexOf() method? Please post any questions you have about what the API doc says that is confusing.maybe I would use indexOf()
Also take a look at the tutorial: http://docs.oracle.com/javase/tutori...aces/list.htmlIf you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
how retrieve and display image from folder
By daisy in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 12-13-2011, 05:14 AM -
Retrieve data from other table
By kichkich in forum JDBCReplies: 7Last Post: 06-23-2011, 09:47 AM -
how to retrieve data from other jtable..
By javanewbie2010 in forum AWT / SwingReplies: 1Last Post: 02-12-2010, 08:32 AM -
speeder way to store and retrieve data
By Ms.Ranjan in forum New To JavaReplies: 3Last Post: 04-21-2009, 05:54 PM -
How to retrieve data from servlet
By valery in forum Java ServletReplies: 1Last Post: 08-06-2007, 08:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks