5 Replies - 263 Views - Last Post: 17 April 2013 - 10:29 PM Rate Topic: -----

#1 BLoweCLE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-April 13

Quick sort, quick lookup, mapping help needed

Posted 16 April 2013 - 07:25 AM

Greetings all,
I could use some help on a program I need to write. The link to the program's instructions is pasted below:

http://grail.cba.csu...ll2013-hw04.pdf

Data required:
http://grail.cba.csu...dians2013in.csv

Here is my thought.. Either created an arraylist or a treemap of Player objects and use this to created the data in the memory. A map would probably be better for sorting, but I'm not very good with them and I don't totally understand how they work. If anyone could give me a push in the right direction I would greatly appreciate it. Some of my code is pasted below:
public class IndiansLookup {

	
	public static void main(String[] args) throws FileNotFoundException {
	
		File file = new File("testdata.txt");
		Scanner scanner = new Scanner(file);
		ArrayList<Player> list = new ArrayList <Player>();
		scanner.nextLine();
		while(scanner.hasNext()){
			String line = scanner.nextLine();
			String[] tokens = line.split(",");
			list.add(makeNewPlayer(tokens));
		}
		
		System.out.println("Enter Player's Last Name: ");
		Scanner scan = new Scanner(System.in);
		String input=scan.next();
		for(int i=0; i<list.size(); i++){
		if(list.get(i).getLastName().compareTo(input)==0){
			System.out.println("match at " + i);
			}
		}

	}

	private static Player makeNewPlayer(String[] tokens){
	return new Player(Integer.parseInt(tokens[0]), tokens[1], tokens[2],
			tokens[3], tokens[4], Integer.parseInt(tokens[5]), Integer.parseInt(tokens[6]),
			Integer.parseInt(tokens[7]),Integer.parseInt(tokens[8]),
			Integer.parseInt(tokens[9]), Integer.parseInt(tokens[10]),
			Integer.parseInt(tokens[11]), Integer.parseInt(tokens[12]),
			Integer.parseInt(tokens[13]), Integer.parseInt(tokens[14]),
			Integer.parseInt(tokens[15]), Integer.parseInt(tokens[16]),
			Double.parseDouble(tokens[17]), Double.parseDouble(tokens[18]), 
			Double.parseDouble(tokens[19]),	Double.parseDouble(tokens[20]));
	}
}




Player class:
public class Player {
	
	private int rank = 0;
	private String lastName = "";
	private String firstName = "";
	private String team = "";
	private String position = "";
	private int gamesPlayed = 0;
	private int atBats = 0;
	private int runs = 0;
	private int hits = 0;
	private int doubles = 0;
	private int triples = 0;
	private int homeruns = 0;
	private int runsBattedIn = 0;
	private int walks = 0;
	private int strikeouts = 0;
	private int stolenBases = 0;
	private int caughtStealing = 0;
	private double battingAverage = .000;
	private double onBasePercentage = .000;
	private double sluggingPercentage = .000;
	private double onBasePlusSlugging = .000;
	

	
	public Player() {
		
	}

	public Player(int rank, String lastName, String firstName,
			String team, String position, int gamesPlayed,
			int atBats, int runs, int hits, int doubles,
			int triples, int homeruns, int runsBattedIn, 
			int walks, int strikeouts, int stolenBases,
			int caughtStealing, double battingAverage,
			double onBasePercentage, double sluggingPercentage,
			double onBasePlusSlugging) {
		this.rank = rank;
		this.lastName = lastName;
		this.firstName = firstName;
		this.team = team;
		this.position = position;
		this.gamesPlayed = gamesPlayed;
		this.atBats = atBats;
		this.runs = runs;
		this.hits = hits;
		this.doubles = doubles;
		this.triples = triples;
		this.homeruns = homeruns;
		this.runsBattedIn = runsBattedIn;
		this.walks = walks;
		this.strikeouts = strikeouts;
		this.stolenBases = stolenBases;
		this.caughtStealing = caughtStealing;
		this.battingAverage = battingAverage;
		this.onBasePercentage = onBasePercentage;
		this.sluggingPercentage = sluggingPercentage;
		this.onBasePlusSlugging = onBasePlusSlugging;
		}

	public String showData(){
		return "Rank: " + this.rank + "\nLast Name: " + this.lastName + "\nFirst Name: " + this.firstName
				+ "\nTeam: " + this.team + "\nPosition: " + this.position + "\nGames Played: " +
				this.gamesPlayed + "\nAt Bats: " + this.atBats + "\nRuns: " + this.runs +"\nHits: "
				+this.hits + "\nDoubles: " + this.doubles + "\nTriples: " + this.triples + "\nHomeruns: "
				+ this.homeruns + "\nRBIs: " + this.runsBattedIn + "\nWalks: " + this.walks + "\nStrikeouts: "
				+this.strikeouts + "\nStolen Bases: " + this.stolenBases + "\nCaught Stealing: " 
				+this.caughtStealing + "\nBatting Average: " + this.battingAverage + "\nOn Base Percentage: "
				+this.onBasePercentage + "\nSlugging Percentage: " + this.sluggingPercentage + "\nOBPS: "
				+this.onBasePlusSlugging;
	}
//getters and setters


This post has been edited by macosxnerd101: 16 April 2013 - 07:28 AM
Reason for edit:: Please remember to use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Quick sort, quick lookup, mapping help needed

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1953
  • View blog
  • Posts: 4,053
  • Joined: 11-December 07

Re: Quick sort, quick lookup, mapping help needed

Posted 16 April 2013 - 07:49 AM

The goal of your data structure is quick lookup. Go back through your notes. Take each data structure in turn and consider how quick the lookup operation is. If it turns out that you don't fully understand the one you need you can either force yourself to learn about it or just do the homework with any old crap.

If it turns out that you need to use maps, they're not difficult conceptually and there are plenty of tutorials about that can help you implement one.
Was This Post Helpful? 0
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • Posts: 1,688
  • Joined: 13-March 10

Re: Quick sort, quick lookup, mapping help needed

Posted 16 April 2013 - 07:52 AM

Sorting is not part of the homework so why would you want to use it? If you want to quickly retrieve objects, checkout Map<K,V> interface.
Was This Post Helpful? 0
  • +
  • -

#4 BLoweCLE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-April 13

Re: Quick sort, quick lookup, mapping help needed

Posted 16 April 2013 - 08:07 AM

Thank you for the quick returns. I will research the MAP<K,V> interface and reply after class if I have any other questions.

LOL Thank you for the quick replies***
Too much java messes with your English
Was This Post Helpful? 0
  • +
  • -

#5 BLoweCLE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-April 13

Re: Quick sort, quick lookup, mapping help needed

Posted 16 April 2013 - 08:24 AM

Here is one part I can't get working:

		System.out.println("Enter Player's Last Name: ");
		Scanner scan = new Scanner(System.in);
		String input=scan.next();
		for(int i=0; i<list.size(); i++){
		if(list.get(i).getLastName().compareTo(input)==0){
			System.out.println("match at " + i);
			}
		}


Here is my compareTo method:

	public int compareTo(Player o2) {
		String name1 = this.getLastName();
		String name2 = o2.getLastName();


		if (name1 == name2)
			return 0;
		else
			return 1;
	}


I have gotten similar methods to work in the past and cannot figure out why it is not working this time.
Was This Post Helpful? 0
  • +
  • -

#6 BLoweCLE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-April 13

Re: Quick sort, quick lookup, mapping help needed

Posted 17 April 2013 - 10:29 PM

I have made great progress on this program and now I'm just one step away from completing it.

package csu.lowe;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/*
 * 
 * 
 */

public class Driver {

	public static void main(String[] args) throws FileNotFoundException {
		
		IndiansLookup indiansMap;
		
		File file = new File("testdata.txt");
		
		Scanner scan = new Scanner(file);
		
		String data = scan.nextLine();
		String[] line = data.split(",");
		
		indiansMap = new IndiansLookup(line);
		
		while (scan.hasNext()){
			data=scan.nextLine();
			String[] tokens = data.split(",");
			String key = new String();
			
			key = tokens[1] + ", " + tokens[2];
			indiansMap.addPlayer(key, tokens);
		}
		
	System.out.println("I will now look up Absdrubal Cabrera");
	indiansMap.playerLookup("Cabrera, A");
	System.out.println("Attempting to look up Shin Soo Choo");
	indiansMap.playerLookup("Choo, S");
	
	indiansMap.getUniqueAverages();
	}
}

And
package csu.lowe;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class IndiansLookup {
	
	//class variables
	private String[] line;
	private Map<String, String[]> playerMap = new HashMap<String, String[]>();
	private Set<String> uniqueAverage = new HashSet<String>();
	
	//constructors
	public IndiansLookup(String[] line){
		this.line=line;
	}
	
	//user defined methods
	public void addPlayer(String name, String[] stats){
		playerMap.put(name,  stats);
	}
	
	public void playerLookup(String name){
		if(playerMap.containsKey(name)){
			String[] stats = playerMap.get(name);
			for (int i=0; i<line.length; i++){
				System.out.println(line[i] + ": " + stats[i]);
			}
		} else System.out.println("Player not found.");
			}
	

	public void getUniqueAverages(){
		for(String[] stats : playerMap.values()){
			uniqueAverage.add(stats[17]);
		}
		System.out.println("Unique batting averages:\n" + uniqueAverage);
	}
}


The part that is not working is the method playerLookup. No matter what I try I get "Player not found."

Thanks again to all who have replied.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1