Java.lang.ArithmeticException: / by zero in LinearProbingHashSet

2

I get ArithmeticException: / by zero when I run my code, how I can correct it. Why I get this error despite of I have 5 array size, when I run debugging I get the error at hash(Key key).

How I can correct the error message please. any one can help me please?

Here is my test class

 public static void main(String[] args) { 
HashElement<Character> he= new HashElement<Character>('p',2);
     HashElement<Character> he1= new HashElement<Character>('a',2);
     HashElement<Character> he2= new HashElement<Character>('c',1);
     HashElement<Character> he3= new HashElement<Character>('z',1);
     HashElement<Character> he4= new HashElement<Character>('d',1);
     LinearProbingHashSet<HashElement> lphs = new LinearProbingHashSet<HashElement>(10);
     
     lphs.insert(he);
     lphs.insert(he1);
     lphs.insert(he2);
     lphs.insert(he3);
     lphs.insert(he4);
     
     System.out.println(lphs);
    
    }

Here is my Class LinearProbingHashSet.java

  public class LinearProbingHashSet <Key>{
  private static int capacity ;
  private int n;          
  private int m;        
  private Key[] a;      
  private double maxLoadFactor;
  private double loadFactor;
public LinearProbingHashSet(int m){
    m = capacity;
    n= 0;  //how many element saved
    a = (Key[]) new Object[m];
    maxLoadFactor = loadFactor;
   }
public LinearProbingHashSet(){
    capacity = 4; 
 }
public int size() {
   return n;
    }
public boolean isEmpty() {
        return size() == 0;
    }
int getCapacity() {
 return m;
    
 }

 public int hash(Key key) {
 return (key.hashCode() & 0x7fffffff) % a.length;
}

void insert(Key key) {
int i;
int hashValue = hash(key); 
if(a[hashValue]==null) {   
    a[hashValue]=key;
}

 if (n >= m/2) resize(2*m);
 for (i = hashValue; a[i] != null; i = (i + 1) % m) {
     if (a[i].equals(key)) { 
            
        return; 
        }
 }
 a[i] = key;

 n++;}
 
public void resize(int capacity) {
LinearProbingHashSet<Key> temp = new LinearProbingHashSet<Key>(capacity);
 for (int i = 0; i < m; i++) {
     if (a[i] != null) {
         temp.insert(a[i]);
     }
 }
 a = temp.a;
 m= temp.m;}}

Here is My HashElement.java Class

    public class HashElement<Key> implements  Comparable<HashElement<Key>> {
    private Key nyckel; //key
    private int counter; //counter
    int freq; 

  public HashElement(Key key) {
    this.nyckel = key;
    freq = 1;
    
    }

public HashElement(Key key, int counter) {
this.nyckel = key;
freq = counter;
 }

public void increment() {
freq++;
}

public void decrement() {
freq--;
 }

int getFrequencey() {
return freq;
}

public Key getKey() {
return nyckel;
}

   public void setKey(Key key) {
   key = nyckel;
   }

 public int compareTo(HashElement that) {
 int counterOne = this.getFrequencey();        
 int counterTwo = that.getFrequencey(); 


 if(counterOne < counterTwo)
     return 1;
  else if(counterOne > counterTwo)
      return -1;
  else
      return 0;  }}
      
java
exception
hash
insert
asked on Stack Overflow May 16, 2021 by Rand • edited May 16, 2021 by Rand

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0