Results 1 to 4 of 4
  1. #1
    MjM8082 is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

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


    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();
    	}
    }
    This is my code right now...[/B]

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

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

  3. #3
    MjM8082 is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: How to retrieve data from a linkedlist and display it?

    Looking at a chart of methods isn't helping to much.
    Often that is the ONLY way to get the answer.
    maybe I would use indexOf()
    What happens when you use the indexOf() method? Please post any questions you have about what the API doc says that is confusing.

    Also take a look at the tutorial: http://docs.oracle.com/javase/tutori...aces/list.html
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. how retrieve and display image from folder
    By daisy in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 12-13-2011, 05:14 AM
  2. Retrieve data from other table
    By kichkich in forum JDBC
    Replies: 7
    Last Post: 06-23-2011, 09:47 AM
  3. how to retrieve data from other jtable..
    By javanewbie2010 in forum AWT / Swing
    Replies: 1
    Last Post: 02-12-2010, 08:32 AM
  4. speeder way to store and retrieve data
    By Ms.Ranjan in forum New To Java
    Replies: 3
    Last Post: 04-21-2009, 05:54 PM
  5. How to retrieve data from servlet
    By valery in forum Java Servlet
    Replies: 1
    Last Post: 08-06-2007, 08:25 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
  •