How do I fix this ArrayIndexOutOfBoundsException in my hashtable?

Yeet McGeet :

I was wondering if anyone could help me this ArrayIndexOutOfBoundsException that occurs when trying to run a tester class.

The exception occurs at the remove method within the hashtable file. I have tried switching out my code with my friend's code but that didn't work either.

Any help would be greatly appreciated

===========================================================================

Here is the HashTable :

public class HashTable {

Object[] hTable;
int mSize;
int size;

HashTable() {
    mSize = 101;
    hTable = new Object[mSize];
}

HashTable(int initCap) {
    mSize = initCap;
}

Object put(Object key, Object value) {
    if (size == mSize) {
        throw new IllegalStateException("No room within the hashtable");
    }

    int hashC = key.hashCode();
    int index = hashC % mSize;

    size++;

    while (index < hTable.length) {
        if (hTable[index] == null) {
            hTable[index] = new Entry(key, value);
            size++;
            return null;

        } else if (((Entry) hTable[index]).key.equals(key)) {
            Object prevVal = ((Entry) hTable[index]).val;
            hTable[index] = new Entry(key, value);
            return prevVal;

        } else if (((Entry) hTable[index]).rCheck) {
            hTable[index] = new Entry(key, value);

            while (index < hTable.length) {
                index++;
                if (hTable[index] == null) {
                    size++;
                    return null;
                } else if (((Entry) hTable[index]).key.equals(key)) {

                    Object prevVal = ((Entry) hTable[index]).val;
                    ((Entry) hTable[index]).remove();

                    return prevVal;
                }
            }
        }
        index++;
    }

    if (hTable[index] == null) {
        hTable[index] = new Entry(key, value);
        return null;

    } else {

        Object oldEntry = ((Entry) hTable[index]).val;
        hTable[index] = new Entry(key, value);
        return oldEntry;
    }

}

Object get(Object key) {
    int hashC = key.hashCode();
    int index = hashC % mSize;

    return ((Entry) hTable[index]).val;
}

Object remove(Object key) {
    int hashC = key.hashCode();
    int index = hashC % mSize;

    Object returnObj = null;

    while (hTable[index] != null) { //here is where the OutOfBounds error occurs
        if (((Entry) hTable[index]).key.equals(key)) {
            returnObj = ((Entry) hTable[index]).val;
            ((Entry) hTable[index]).remove();

            size--;
            break;
        }
        index++;
    }
    return returnObj;
}

int size() {
    return size;
}

@Override
public String toString() {
    String returnString = "";

    for (int i = 0; i < hTable.length; i++) {
        if (hTable[i] == null || ((Entry) hTable[i]).rCheck) {
            returnString += "dummy\n";
            continue;
        }

        returnString += "Index: " + i +
                " \n Key: " + ((Integer) (((Entry) hTable[i]).key)).intValue() % 101 +
                "\nValue: " + (String) (((Entry) hTable[i]).val) +
                "\n++++++++++++++++++++++++++++++++++++++++++++++\n";
    }

    return returnString;
}

private class Entry {
    Object key;
    public boolean rCheck;
    public Object val;

    Entry() {
        key = null;
        val = null;
        rCheck = false;
    }

    Entry(Object k, Object v) {
        key = k;
        val = v;
        rCheck = false;
    }

    Object value() {
        return val;
    }

    Object key() {
        return key;
    }

    void remove() {
        rCheck = true;
    }

    public String toString() {
        return "";
    }
}
}

Here is the hash table tester:

    import java.io.*;
    import java.util.*;

public class hashTest {
    public static void main(String args[]) throws FileNotFoundException {
        HashTable hashTable = new HashTable();
        Scanner fileRead = new Scanner(new File("data1.txt"));
        while(fileRead.hasNext()) {
            Object key = fileRead.next();
            fileRead.next();
            Object value = fileRead.nextLine();
            hashTable.put(key, value);
            System.out.println(hashTable.get(key));
        }
        Scanner fileRead2 = new Scanner(new File("data2.txt"));
        while(fileRead2.hasNext()){
            Object key = fileRead2.next();
            hashTable.remove(key);
            fileRead2.nextLine();

        }
        Scanner fileRead3 = new Scanner(new File("data3.txt"));
        while(fileRead3.hasNext()){
            Object key = fileRead3.next();
            fileRead3.next();
            Object value = fileRead3.nextLine();
            hashTable.put(key, value);

        }
        Scanner fileRead4 = new Scanner(new File("data4.txt"));
        while(fileRead4.hasNext()){
            Object key = fileRead4.next();
            fileRead4.next();
            Object value = fileRead4.nextLine();
            hashTable.put(key, value);

        }
    }
}

===========================================================================

In the shared google drive link below you will find a zip containing data inputs.

https://drive.google.com/file/d/1iYrzWl9mtv_io3q7K1_m2EtPFUXGbC3p/view?usp=sharing

tobsob :

Your Problem is at this code block:

 while (index < hTable.length) {
    index++;
    if (hTable[index] == null)...

At the last iteration index will be the same as hTable.length. In your Example index will be 100 where the condition will be accepted. In the next step index will be incremented: index = 101. At hTable[101] the ArrayIndexOutOfBoundsException will occur.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=357273&siteId=1
Recommended