java - I am getting a StackOverflow error in a hashfunction code but I cannot determine , can someone help me fix it/ -


i creating hash function myself university assignment. hash function works ... take string input , add ascii values of every character integer variable named sum. done in function named hash_func. in function named myhashfunc have used recursion decrease value of sum such can value lesser size of array in store data in using hash function. since using seperate chaining method resolve collisions , used linkedlist array. getting stack overflow error when function hash_func called inside myhashfunc. code given below:-

package hashfunction;  import java.util.linkedlist; import java.util.scanner;  public class hashfunction {  public static int myhashfunc(string str,int a){     int x=0;     int sum = hash_func(str);     if(sum<a)         return sum;     else{         x = x+sum%10;         sum /= 10;         return(myhashfunc(str, a));     } }  public static int hash_func(string str) {     int sum = 0;     int len = str.length();     (int = 0; < len; i++) {         if (str.charat(i) >= '0' && str.charat(i) <= '9') {             sum += (int) str.charat(i);         } else if (str.charat(i) >= 'a' && str.charat(i) <= 'z' ||           str.charat(i) >= 'a' && str.charat(i) <= 'z') {             sum += (int) str.charat(i);         }     }    return sum; }  public static void main(string[] args) {     scanner sc = new scanner(system.in);     int n;     int z;     n = sc.nextint();     string[] str_array = new string[n];     linkedlist<string>[] l_list = new linkedlist[n];     (int = 0; < n; i++) {         l_list[i] = new linkedlist<string>();     }     (int = 0; < n; i++) {         str_array[i] = sc.next();     }     (int = 0; < n; i++) {         z = myhashfunc(str_array[i],n);         if(l_list[z].peek()!="-1"){                 l_list[z].set(z, str_array[i]);         }         else{             l_list[z].add(str_array[i]);         }     }      (int = 0; < n; i++) {         int size = l_list[i].size();           (int j = 0; j < size; j++) {               system.out.println(l_list[i].get(j));         }     } } } 

in method

public static int myhashfunc(string str,int a){     int x=0;     int sum = hash_func(str);     if(sum<a)         return sum;     else{         x = x+sum%10;         sum /= 10;         return(myhashfunc(str, a));  // call again myhashfunc same parameters     } } 

if sum >= a enter else block , call again same method same parameters. generate stackoverflow.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -