Stack Overflow in recursive function, java

I get a stack overflow error with the folowing function. The main point is binary search. The "afficher()" function replacesSystem.out.print, and the triAlphab() function return the lowest of two string in the alphabetical order. These two work fine. Thanks in advance.

public static int nomPresentDansLaBase( String motCherche , String[] tableau , int min, int max) {          int mediane = (max - min) / 2;     // La médiane sera ensuite utilisée pour définir le prochain intervalle           //Le cas ou l'on trouve ce que l'on cherchait et donc on renvoie l'indice auquel on l'a trouvé     if(tableau[mediane].equals(motCherche) )         return mediane;          //Cela veut dire que l'on a fini de parcourir le tableau     if (min>=max)         return -1 ;                    if( triAlphab( motCherche , tableau[mediane], 0 ) == 2 ) {          afficher("Moyenne basse");         return nomPresentDansLaBase( motCherche, tableau, mediane+1, max);     }     else {         afficher("Moyenne haute");         return nomPresentDansLaBase( motCherche, tableau, min -1 , mediane); }// EO fonction nomPresentDansLaBase 

I get the following error : at fr.gaetan.algoDichotomie.Principale.nomPresentDansLaBase(Principale.java:124)

like 100 times or more. L124 is this one :

            return nomPresentDansLaBase( motCherche, tableau, mediane+1, max); 

I call the

I call the function here in my main :

afficher("Quel nom cherchez vous ?"); motCherche = sc.nextLine(); int indice = nomPresentDansLaBase( motCherche, bdd, 0, bdd.length-1 ); 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.