0

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

  • 2
    Welcome to StackOverflow!! You have not shared sufficient information. Please post a [mre] that demonstrates the problem. The code should compile and run **or** give the same compilation error that you are getting. And please include explanations as to what exactly you are trying to accomplish which includes how the weights are to be used, the `LPHashTable` class, sample, input data, etc. And keep it as simple as possible. And also take the [tour] and read [ask] so you may better participate on this site. – WJS May 13 '23 at 19:48
  • it gave me the following error when i tried to run it : "Exception in thread "main" java.lang.Error: Unresolved compilation problem: The operator += is undefined for the argument type(s) int, void at Optimize.main(Optimize.java:58)" . The LPHash uses a hash table and linear probing to arrange data inserted into the table. The output it should be giving is "44 4" which represents that the least number of probes to insert all the usernames (which is the data) was 44 and there were 4 different combinations (sets of 9) that achieved a probe of count 44. – user21893624 May 13 '23 at 21:05
  • 2
    Please post the [mre] as requested. – WJS May 13 '23 at 22:28
  • I think I've added what I think is the minimal reproducible example sorry I am a bit confused by that but that is all the information about the question. – user21893624 May 15 '23 at 13:18
  • I flagged your question and the moderators saw fit to undelete it. In general, please don't be deleting your question when answer(s) have been provided. Your question may also help others with a similar problem. You can ignore the answer and wait for a better one, up vote it if you have 15+ rep, or click the accept check mark to show that it was, in your opinion, the best that addressed your particular problem. – WJS May 17 '23 at 11:28

1 Answers1

1

The operator += is undefined for the argument type(s) int, void at Optimize.main(Optimize.java:58)" .

The error is a compile time error. And since you didn't include the LPHashTable I can only guess. It appears you are doing the following:

numProbes += hashTable.insert(name);

If the insert method is declared as public void insert(String name) it isn't returning anything (void return declaration) so it can't add to numProbes. The compiler knows this and complains. It should be declared as public int insert(String name) so it returns an int value.

And here is another suggestion. You don't need all the nested loops. You can "increment" an array from right to left by using a simple method. I made it fairly general so you can specify an array of limits to control each location's maximum value.

int[] limits = {4,4,4,4,4,4,4,4,4};
int[] weights = new int[9];
for (int i = 0; i < 2000; i++) {
    System.out.println(Arrays.toString(weights));
    increment(weights, limits);
} 
    
public static void increment(int[] weights, int[] limits) {
    for (int i = weights.length-1; i >= 0; i--) {
        if (weights[i] < limits[i]) {
            weights[i]++;
            return;
        }
        weights[i] = 0;
    }
    return;
}

prints

[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 0, 2]
[0, 0, 0, 0, 0, 0, 0, 0, 3]
[0, 0, 0, 0, 0, 0, 0, 0, 4]
[0, 0, 0, 0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 0, 0, 0, 1, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 2]
[0, 0, 0, 0, 0, 0, 0, 1, 3]
[0, 0, 0, 0, 0, 0, 0, 1, 4]
[0, 0, 0, 0, 0, 0, 0, 2, 0]
...
...
[0, 0, 0, 0, 3, 0, 4, 3, 3]
[0, 0, 0, 0, 3, 0, 4, 3, 4]
[0, 0, 0, 0, 3, 0, 4, 4, 0]
[0, 0, 0, 0, 3, 0, 4, 4, 1]
[0, 0, 0, 0, 3, 0, 4, 4, 2]
[0, 0, 0, 0, 3, 0, 4, 4, 3]
[0, 0, 0, 0, 3, 0, 4, 4, 4]
WJS
  • 36,363
  • 4
  • 24
  • 39