java - Displaying search results from a hash table -


i can't figure out how display results search method. put strings in needs display results.

here client class

public class client {     private string name;     private string city;      public client(string name, string city)     {         this.name = name;         this.city = city;     }      public string tostring()     {         return name + " " + city;     }      public string getname()     {         return name;     }      public string getcity()     {         return city;     } } 

here hashtable class

public class hashtable {     private int n;     private client[] table;      public hashtable(int n) {         this.n = n;         table = new client[n];     }      public int hashfunction(string key) {         int sum = 0;         (int = 0; < key.length(); i++) {             sum += (int) key.charat(i);         }         sum = sum%n;         return sum;     }      public string search(string key) {                int sum = 0;         (int = 0; < key.length(); i++) {             sum += (int) key.charat(i);         }         sum = sum%n;         if (key.equals(table[sum])) {             return ("returns tostring client class here");         } else if (table[sum] == null) {             return null;         } else {             while (table[sum] != null) {                 sum++;             }             return ("returns tostring client class here");         }     }      public boolean insert(client myclient) {         int counter = 0;         string temp = myclient.getname();         boolean ret = false;         int tempsum = 0;         (int = 0; < temp.length(); i++) {             tempsum += (int) temp.charat(i);         }         tempsum = tempsum%n;         if (table[tempsum] == null) {             table[tempsum] = myclient;             ret = true;         } else {             while (table[tempsum] != null) {                 if(tempsum == table.length){                     tempsum = -1;                 }                 tempsum++;                 counter++;             }             if(counter != n){                 ret = true;                   table[tempsum] = myclient;             }         }          return ret;     } } 

when comparing key in search method, make sure compare against name.

change table[sum] table[sum].getname() ???? modified code , added comments change.

public class hashtable {     private int n;     private client[] table;      public hashtable(int n) {         this.n = n;         table = new client[n];     }      public int hashfunction(string key) {         int sum = 0;         (int = 0; < key.length(); i++) {             sum += (int) key.charat(i);         }         sum = sum%n;         return sum;     }      public string search(string key) {                int sum = 0;         (int = 0; < key.length(); i++) {             sum += (int) key.charat(i);         }         sum = sum%n;         if (key.equals(table[sum].getname())) {  //this should table[sum].getname()             return ("returns tostring client class here");         } else if (table[sum] == null) {             return null;         } else {             while (table[sum] != null) {                 sum++;             }             return ("returns tostring client class here");         }     }      public boolean insert(client myclient) {         int counter = 0;         string temp = myclient.getname();         boolean ret = false;         int tempsum = 0;         (int = 0; < temp.length(); i++) {             tempsum += (int) temp.charat(i);         }         tempsum = tempsum%n;         if (table[tempsum] == null) {             table[tempsum] = myclient;             ret = true;         } else {             while (table[tempsum] != null) {                 if(tempsum == table.length){                     tempsum = -1;                 }                 tempsum++;                 counter++;             }             if(counter != n){                 ret = true;                   table[tempsum] = myclient;             }         }          return ret;     } } 

let me know if helped


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -