I'm meant to be computing weights that optimise the insertion of a specific list of names into a hash table using linear probing. I ran into a problem when trying to use "+=" with a void and int value. Can anyone help with letting me know where my code went wrong as the hashTable.insert method is a void :
public class Optimize {
public static void main(String[] args) throws IOException {
String fileName = "mydata.txt";
String[] names = readCustomList(fileName);
int numNames = names.length;
int[] weights = new int[9];
int numWeightCombinations = 0;
int leastNumProbes = Integer.MAX_VALUE;
for (int w0 = 0; w0 <= 4; w0++) {
weights[0] = w0;
for (int w1 = 0; w1 <= 4; w1++) {
weights[1] = w1;
for (int w2 = 0; w2 <= 4; w2++) {
weights[2] = w2;
for (int w3 = 0; w3 <= 4; w3++) {
weights[3] = w3;
for (int w4 = 0; w4 <= 4; w4++) {
weights[4] = w4;
for (int w5 = 0; w5 <= 4; w5++) {
weights[5] = w5;
for (int w6 = 0; w6 <= 4; w6++) {
weights[6] = w6;
for (int w7 = 0; w7 <= 4; w7++) {
weights[7] = w7;
for (int w8 = 0; w8 <= 4; w8++) {
weights[8] = w8;
LPHashTable hashTable = new LPHashTable(37);
hashTable.setWeights(weights);
int numProbes = 0;
for (String name : names) {
numProbes += hashTable.insert(name);
}
if (numProbes < leastNumProbes) {
leastNumProbes = numProbes;
numWeightCombinations = 1;
}
else if (numProbes == leastNumProbes) {
numWeightCombinations++;
}
}
}
}
}
}
}
}
}
}
System.out.println(leastNumProbes + " " + numWeightCombinations);
}
/**
* Reads usernames from data text file and inserts them into
* an ArrayList.
*/
private static String[] readCustomList(String filename) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
StringBuilder sb = new StringBuilder();
while((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
String[] names = sb.toString().split("\\s+");
return names;
}
}
And here is the hashTable.insert method which is a void :
public void insert(String key) {
int index = findIndex(key);
if (index==-1) {
// Table is full, time to rehash.
this.rebuild();
}
if (table[index]==null) {
table[index]= key;
this.entries++;
}
}
I tried this code above but it kept giving me the error of using a void and int types for the "+=". I also tried just incrementing the probeCount and then inserting to the hash table but it gave me the wrong values for leastNumProbes and numWeightCombinations.
Here is the dataset of mydata.txt which is the usernames im meant to be linear probing :
dfferi001
abdibr008
esnchr001
cmpcla004
phlzwi002
gngjoa003
zndkho003
krnnen001
njkabo001
blmjoh007
brcros003
tgdmoe001
ndlzan021
dlkdim001
pthkis001
mtlmar020
mshrum006
ntlasa006
ndzmas002
rsbtsh001
chtkea005
rnbluk001
mdzwav001
ngqabo003
strrac002
cbxlis001
schpie019
chtsha051
ersand012
mgtlut001
mssoli002
mdxnde001
vlnsha004
krnern002
krrfar003
rppkei001
and using Optimize.java it produces : 12 1953125