1

For my assignment we need to create a HashTable of Strings, I've created one using integers and it works perfectly, but I'm running into trouble now.

I keep getting java.lang.NumberFormatExceptions for my inputted Strings and I'm not sure why this is happening. Here's the code I am using for my HashTable class;

public class HashTable { private SortedList[] hashArray; private int arraySize; public HashTable(int size) { arraySize = size; hashArray = new SortedList[arraySize]; for(int i = 0; i < arraySize; i++) { hashArray[i] = new SortedList(); } } // END HashTable() public void displayTable() { for(int i = 0; i < arraySize; i++) { System.out.print(i + ". "); hashArray[i].displayList(); } } // END displayTable() public int hash(String key) { int hashkey = Integer.parseInt(key); return hashkey % arraySize; } // END hash() public void insert(Link link) { String key = link.getKey(); int hashVal = hash(key); hashArray[hashVal].insert(link); } // END insert() public void delete(String key) { int hashVal = hash(key); hashArray[hashVal].delete(key); } // END delete() } // END HashTable 

This is the code for the List:

public class SortedList { private Link first; public void SortedList() { first = null; } //END SortedList() public void insert(Link link) { String key = link.getKey(); Link previous = null; Link current = first; while(current != null && key.compareTo(current.getKey()) > 0) { previous = current; current = current.getNext(); } if(previous == null) { first = link; } else { previous.setNext(link); link.setNext(current); } } // END insert() public void delete(String key) { Link previous = null; Link current = first; while(current != null && !key.equals(current.getKey())) { previous = current; current = current.getNext(); } if(previous == null) { first = first.getNext(); } else { previous.setNext(current.getNext()); } } // END delete() public Link find(String key) { Link current = first; while(current != null && current.getKey().compareTo(key) <= 0) { if(current.getKey().equals(key)) { return current; } current = current.getNext(); } return null; } // END find() public void displayList() { System.out.print("List (first -> last): "); Link current = first; while(current != null) { current.displayLink(); current = current.getNext(); } System.out.println(); } // END displayList() } //END SortedList() 

The Link class is just a basic one, I get thrown the error on the line:

 int hashkey = Integer.parseInt(key); 

In the hash function, in my HashTable class.

EDIT: From what I understand that I am being asked to do, I need to convert the String into an integer, and then perform the hash function to get the index for its position.

2
  • I think you need to write a hash function from string -> int instead of trying to interpret the key as a string of decimal digits which it may not be. Commented Jul 15, 2013 at 15:52
  • You don't need to "convert the String into an integer". You need to obtain an integer that is likely to be different for different strings, and will definitely be the same for strings with the same sequence of characters. The String hashCode() result is ideal for this. Commented Jul 15, 2013 at 16:12

4 Answers 4

3

Because your link.getKey() and key variables in Hashtable.insert and Hashtable.delete allow all Strings to be passed. You should probably be using

public int hash(String key) { int hashkey = key.hashCode(); return hashkey % arraySize; } // END hash() 
Sign up to request clarification or add additional context in comments.

Comments

1

To convert the String key into an integer for the purposes of use a hash key, use key.hashCode().

Comments

1

Looks like you are trying to convert a string into Integer here, where the string is not actually representing a number

 int hashkey = Integer.parseInt(key); 

If you would like to get the hashcode for String key, you can simply call

int hashkey = key.hashCode(); 

Comments

1

You're passing strings which aren't valid integer strings. As per the Integer#parseInt() JavaDoc:

Throws:

NumberFormatException - if the string does not contain a parsable integer.

Find a different way to turn a string into an integer. Hint: String#hashCode().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.