for assignment need create hashtable of strings, i've created 1 using integers , works perfectly, i'm running trouble now.
i keep getting java.lang.numberformatexceptions
inputted strings , i'm not sure why happening. here's code using hashtable class;
public class hashtable { private sortedlist[] hasharray; private int arraysize; public hashtable(int size) { arraysize = size; hasharray = new sortedlist[arraysize]; for(int = 0; < arraysize; i++) { hasharray[i] = new sortedlist(); } } // end hashtable() public void displaytable() { for(int = 0; < 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 code 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 basic one, thrown error on line:
int hashkey = integer.parseint(key);
in hash function, in hashtable class.
edit: understand being asked do, need convert string integer, , perform hash function index position.
because link.getkey()
, key
variables in hashtable.insert
, hashtable.delete
allow strings
passed. should using
public int hash(string key) { int hashkey = key.hashcode(); return hashkey % arraysize; } // end hash()
Comments
Post a Comment