This just shows the very basics of hash tables, with console based input only, and using primitives only also.
/Users/adelaide/Public/Netbeans - All JSR Projects/Hashing 2012/src/hashing2012package/HashingMainConsole.java |
1 package hashing2012package; 2 3 import java.io.BufferedReader; 4 import java.io.InputStreamReader; 5 6 /** 7 * 8 * @author John Rayworth 9 */ 10 public class HashingMainConsole { 11 12 private static String [] hashTable = new String[10]; 13 14 public static void main(String[] args) { 15 16 try{ 17 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 18 System.out.println("What name would you like to add to the hash table?"); 19 String readIn = br.readLine(); 20 addToHashTable(readIn); 21 22 System.out.println("What name would you like to add to the hash table?"); 23 readIn = br.readLine(); 24 addToHashTable(readIn); 25 26 System.out.println("What name would you like to add to the hash table?"); 27 readIn = br.readLine(); 28 addToHashTable(readIn); 29 30 System.out.println("What name would you like to find in the hash table"); 31 String nameToFind = br.readLine(); 32 33 if(findName(nameToFind)){ 34 System.out.println("Yes, it's in the hash table."); 35 }else{ 36 System.out.println("Nope, not in the hash table"); 37 } 38 39 40 }catch(Exception e){ 41 System.out.println("Error."); 42 } 43 } 44 45 46 private static void addToHashTable(String stringToAdd){ 47 int total = 0; 48 for(int i = 0; i < stringToAdd.length(); i++){ 49 total += stringToAdd.charAt(i); 50 } 51 int hashValue = total % 10; 52 hashTable[hashValue] = stringToAdd; 53 } 54 55 56 private static boolean findName(String stringToFind){ 57 int total = 0; 58 for(int i = 0; i < stringToFind.length(); i++){ 59 total += stringToFind.charAt(i); 60 } 61 int hashValue = total % 10; 62 if(hashTable[hashValue].equals(stringToFind)){ 63 return true; 64 } 65 else{ 66 return false; 67 } 68 } 69 70 } 71 72