0

So I need to write this program that receives 17 files that contain NFL team names and scores (like one file contains scores for all 32 teams, while another file may contain 30 different scores for 30 of the same teams, but omitting two teams of course). And my professor provided us with a HashTable implementation to use, and it handles the collisions by creating some sort of LinkedList at each occupied index in the HashTable (I'm fairly inexperienced, so I'm sorry if I don't get the all the terminology correct, but hopefully you know what I mean). I have successfully imported all the files and data and have inputted them into the HashTable with the collision handling that my professor gave us. However, whenever I try to call the get method for any of the keys, it returns "null". Why is this? I ask because I need to find the average team score for each team, and I can't figure out to do this because the get method is returning null. Any help would be much appreciated!

Code:

HashEntry:

public class HashEntry 
{
private String key;
private Double value;
private HashEntry next;

public HashEntry(String key, Double value) 
{
    this.key = key;
    this.value = value;
}

public String getKey() 
{
    return key;
}

public void setKey(String key) 
{
    this.key = key;
}

public Double getValue() 
{
    return value;
}

public void setValue(Double value) 
{
    this.value = value;
}

public HashEntry getNext() 
{
    return next;
}

public void setNext(HashEntry next) 
{
    this.next = next;
}

public boolean isNextEmpty()
{
    if(next.equals(null))
        return true;
    return false;
}

HashTable:

public class HashTable implements StringHashTable 
{
private HashEntry[] dataArray;
private int size;

public HashTable() 
{
    dataArray = new HashEntry[1000];
    size = 0;
}

private int hash(String key) 
{
    int sum = 0;
    for(int i = 0; i < key.length(); i++)
        sum += (int)key.charAt(i);

    return sum % dataArray.length;
}

@Override
public void put(String key, Double value) 
{
    HashEntry entry = new HashEntry(key, value);
    int indexToPut = hash(key);
    HashEntry cursor = dataArray[indexToPut];
    if(cursor != null) 
    {
        while(cursor.getNext() != null && cursor.getKey() != key) 
        {
            cursor = cursor.getNext();
        }
        if(cursor.getKey() != key) 
        {
            cursor.setNext(entry);
        } 
        else 
        {
            cursor.setValue(value);
        }
    } 
    else 
    {
        dataArray[indexToPut] = entry;
    }
    size++;
}

@Override
public Double get(String key) 
{
    int indexToGet = hash(key);
    HashEntry cursor = dataArray[indexToGet];
    while(cursor != null && cursor.getKey() != key) 
    {
        cursor = cursor.getNext();
    }
    if (cursor == null) 
    {
        return null;
    }
    return cursor.getValue();
}

@Override
public int size() 
{
    return size;
}

@Override
public void remove(String key) 
{
    int indexToRemove = hash(key);
    HashEntry cursor = dataArray[indexToRemove];
    HashEntry prev = null;
    while(cursor != null && cursor.getKey() != key) 
    {
        prev = cursor;
        cursor = cursor.getNext();
    }
    if (cursor != null) 
    {
        if (prev == null) 
        {
            dataArray[indexToRemove] = cursor.getNext();
        } 
        else 
        {
            prev.setNext(cursor.getNext());
        }
        size--;
    }
}

public String toString() 
{
    String res = "";
    for(HashEntry entry : dataArray) 
    {
        if (entry != null) 
        {
            HashEntry cursor = entry;
            while(cursor != null) 
            {
                res += cursor.getKey() + " = " + cursor.getValue() + "\n";
                cursor = cursor.getNext();
            }
        }
    }
    return res;
}

Driver Class:

public class Project3 
{
static HashTable table = new HashTable();   
static HashMap<String, Double> table1 = new HashMap<String, Double>();
public static void main(String[] args) throws IOException
{
    //HashTableImpl<String, Double> table = new HashTableImpl<String, Double>();

    if (args.length < 1) 
    {
        System.out.println("Error: Directory name is missing");
        System.out.println("Usage: java scoreProcess directory_name");
        return;
    }

    File directory = new File(args[0]); // args[0] contains the directory name
    File[] files = directory.listFiles(); // get the list of files from that directory

    File file;
    Scanner input;

    // process the arguments stores in args
    for (int i = 0; i < files.length; i++) 
    {
        input = new Scanner(files[i]);

        //System.out.println("\nCurrent file name: " + files[i].getName());

        // no error checking done here, add your own
        String name;
        Double score;
        while(input.hasNext())
        {
            name = "";
            while(!input.hasNextDouble())
            {
                name += input.next() + " ";
            }
            score = input.nextDouble();
            //System.out.println("Name: " + name + " Score: " + score);
            table.put(name, score);
            table1.put(name, score);
        }
    }
    System.out.println("\n");
    System.out.println(table.toString());
    System.out.println(table.size());
    //System.out.println(table1.toString());
    System.out.println(table.get("Minnesota"));
}
}

Driver Output: https://drive.google.com/file/d/0BwujWiqVRKKsNW52N1M2UllCeHc/view?usp=sharing

Example Text File:

New England 27
Indianapolis 24
Tennessee 17
Miami 7
St. Louis 17
Arizona 10
Seattle 21
New Orleans 7
NY Jets 31
Cincinnati 24
Pittsburgh 24
Oakland 21
Washington 16
Tampa Bay 10
San Diego 27
Houston 20
Jacksonville 13
Buffalo 10
Detroit 20
Chicago 16
Cleveland 20
Baltimore 3
Atlanta 21
San Francisco 19
Philadelphia 31
NY Giants 17
Minnesota 35
Dallas 17
Denver 34
Kansas City 24
Green Bay 24
Carolina 14

1 Answers1

0

You have a number of errors in your code. The most obvious I can see on first read are:

  • compare strings with equals not == in the implementation of map
  • don't add a space at the end of the name before you put it in the map

My main advice to you looking at your code is to learn to develop unit test in conjunction with writing your code. In this case you should have tests that show that HashEntry does what you expect before using it in HashTable which should also be thoroughly tested before you proceed to reading values from files and putting them in the map. Or you can do it in the opposite order if you use mocking. But trying to test at the end makes it much more difficult to know what has gone wrong. Learn to build unit tests (preferably before you write the code) and these types of problems will be much easier to find and resolve.

sprinter
  • 27,148
  • 6
  • 47
  • 78
  • Thank you for the advice, I will definitely take your advice into consideration for future projects, as I can totally see why that would help. So in regards to the equals method, I should change all the boolean expressions in the get method located in "HashTable" to .equals(), correct? – robertparsons4 Jul 22 '16 at 04:20
  • So I replaced the == with .equals() and removed the space at the end of the name, and now the get method returns the first value for each team, which is much better than what I was getting! Thank you so much! – robertparsons4 Jul 22 '16 at 04:28