#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    20
    Rep Power
    0

    Stuck on a program - LinkedList


    I've almost completed this program but I have no idea what to do next. I'm stuck. I'm going through a beginners book for Java and this is one of the practice programs.

    Here is what I have ... The problem I'm having is in the two spots where there is //code comments. The user will enter NFL stadiums and then the game revenue. then when they hit done... they can type a stadium and then the revenue will be displayed. I got all the user input stuff down, but I have no idea how to check the LinkedList for a stadium match and display the revenue.


    Here is what I got...



    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();
    	}
    }


    I'm looking for an output like this...

    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
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    20
    Rep Power
    0
    My output now looks like this...


    On each line enter the stadium name and game revenue.
    Enter done when you are finished.
    Enter thhe name of the stadium:giants
    Enter game revenue: 1000
    Enter thhe name of the stadium:done
    Enter thhe name of the stadium to get the total revenue for it.
    giants

    There is no stadium named giants
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Have you looked at the API doc for the LinkedList class to see if it has any methods that will help you?


    Also posted at http://www.java-forums.org/new-java/62819-how-retrieve-data-linkedlist-display.html
    Last edited by NormR; September 7th, 2012 at 09:22 PM.

IMN logo majestic logo threadwatch logo seochat tools logo