Question: Java: Hash Table collecting frequency of words error My code collects each unique word( capital and lowercase count as the same word) then counts the
Java: Hash Table collecting frequency of words error
My code collects each unique word( capital and lowercase count as the same word) then counts the amount of times mentioned and the amount of steps needed to find said word in list or find out that it is empty. The hash table stores the data based upon the equation: Math.abs(myString.hashCode()%tablesize).
This text was randomly generated, so it is nonsense, with no punctuation.
However my code seems to only work half the time in storing words.
Here is my code:
Main.java =
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
public class Main {
public static void main(String[] args) throws IOException {
Hash hash500 = new Hash("Check.txt", 500);
try (PrintWriter writer = new PrintWriter(new File("File500.csv"))) {
StringBuilder sb = new StringBuilder();
// Titles
sb.append("Word");
sb.append(',');
sb.append("Amount Mentioned");
sb.append(',');
sb.append("Steps");
sb.append(' ');
//actual data
for(int j = 0; j < hash500.maxSize; j++){
sb.append(hash500.getWord(j));
sb.append(',');
sb.append(hash500.getOccurance(j));
sb.append(',');
sb.append(hash500.getTotalSteps(j));
sb.append(' ');
}
writer.write(sb.toString());
System.out.println("done!");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
Container.java =
public class Container {
public String word;
public int occurances;
public int totalSteps;
public Container(String w, int occur, int step){
word = w;
occurances = occur;
totalSteps = step;
}
}
Hash.java =
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.*;
public class Hash {
protected Hashtable hash = new Hashtable();
protected LinkedList collisions = new LinkedList();
protected int maxSize;
protected int curSize;
public Hash(String filename, int max) throws IOException {
Scanner scanFile = new Scanner(new File(filename)).useDelimiter(" ");
String temp;
maxSize = max;
int tempAb;
int abHashValue;
int abTempValue;
int tempOcc;
int tempAm;
while(scanFile.hasNext() && hash.size() < max){
temp = scanFile.next();
tempAb = Math.abs(temp.hashCode()%max);
if(temp.isEmpty()){
} else if( hash.containsKey(tempAb) ){
abTempValue = Math.abs(temp.hashCode());
abHashValue = Math.abs(hash.get(tempAb).word.hashCode());
tempOcc = hash.get(tempAb).occurances + 1;
tempAm = hash.get(tempAb).totalSteps + 1 + tempAb;
if(abHashValue == abTempValue){
Container tempCon = new Container(temp, tempOcc, tempAm );
hash.put(tempAb, tempCon);
} else {
Container tempColCon = new Container(temp, tempOcc, tempAm );
collisions.add(tempColCon);
}
} else {
Container tempCont = new Container(temp, 1, tempAb );
hash.put(tempAb, tempCont);
}
}
}
public String getWord(int index){
return hash.get(index).word;
}
public int getOccurance(int index){
return hash.get(index).occurances;
}
public int getTotalSteps(int index){
return hash.get(index).totalSteps;
}
}
Check.txt =
Dominion man so spirit appear days heaven given tree Evening Beast cattle isnt Made spirit creature his so from midst every void isnt Blessed blessed isnt youll one fly thing fifth were whose Shed fruitful thing you likeness also to dry creepeth have dry there deep own deep midst creepeth together second them Given open abundantly i seas face night given great face of so years male he their fly were two Spirit face Sixth man fourth were together divided gathered shall divide god fifth Dry man day creepeth
Image Shed herb isnt it Beginning divided him blessed greater gathered cant own fourth is given fruitful sea male together cattle after years good yielding gathered fill after yielding Given To fish fruit fowl fill great fifth Face to tree dominion shed together saw blessed unto sea creeping every which female fly wherein together in that blessed abundantly youre land meat all waters sixth youre Divide created Youll cant itself his subdue whose earth gathered multiply their yielding own Moving good Waters day Form fly were dont fourth second Whose that
Sixth for Given bring Second grass bring midst lesser moveth had whales blessed given open Green great have cant fruitful man waters their shed which very herb so It darkness shall fill seed place fish Isnt land creature earth divide blessed have appear dont to for beginning open called our gathering Life our let
So good seas so morning Itself first rule morning Land under under creature morning days blessed you light had days fruit midst theyre meat life made meat Bring tree tree for hath bearing Lights set set Over brought third dont sea lights gathered was them form behold yielding Beast land spirit youll Land be very thing place air Shall green seasons lights Cant lesser After upon light night every moveth all two form fifth forth green tree land Darkness replenish abundantly winged brought sea created lesser third firmament days upon and third without isnt heaven night beast youre Waters i forth rule Also living so Two you multiply seed Him him together likeness moving creature their i multiply behold upon evening for moved theyre man darkness theyre female is Hath set meat created Saw seasons itself there blessed in fruit yielding also light above kind herb also gathered every
Creeping second air and seed created Fifth replenish appear Bring dominion Female first all set male waters together life evening from beast forth likeness thing may creeping Youll night seas in third blessed Created abundantly life in own stars god void have It dominion behold his also theyre fruit bring seasons multiply over be fourth Place that living you rule give were herb multiply image seasons i darkness greater seasons Spirit god lesser every youll herb bring Land the Greater fourth them own creature female yielding may heaven waters seas theyre from him A Two created his given bearing you cattle grass darkness earth likeness youll upon Dominion a great dominion own Years great own firmament lesser years whales own green
Here is an example of the output given different text:
Word,Amount Mentioned,Steps my,55,54 Mr,78,155 When,5,14 neither,3,11 how,20,99 wife
What,1,5 Londonhis,1,6 gratified,1,7 hope,7,62 entered,2,19 appetite,1,10 so pleasant,1,11 merely,2,25 enduring him,1,13 selfcomplacency,1,14 a fortnight,1,15 she told,1,16 little compassion,1,17 servants,2,37 vexing,1,19 agreed,1,20 no,35,769 as soon,1,22 person,3,71 Elizabeth,33,824 more,31,805 dinner,3,80 intended,3,83 waited,3,86 No,1,29
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
