Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to use a recursive method that checks the array and see if there are suitable people, if there is, it returns the amount of them.

<pre>import java.util.Arrays;

public class HavreiKnesetMain {
	public static int isSuitable(HavreiKneset [] arr, String miflaga, boolean migdar) {
		int count=0;
		int index=0;
		if(index>=arr.length) {
			return 1;
		}
		else {
			index++;
			if(arr[index].getNameOfMiflaga().equals(miflaga) && arr[index].isGender()==migdar) {
				count++;
			}
			return count + isSuitable(arr,miflaga,migdar);
		}
	}
	public static void main(String[] args) {
		HavreiKneset [] arr = new HavreiKneset[4];
		HavreiKneset h1 = new HavreiKneset("Nissim", "meretz",true, true);
		HavreiKneset h2 = new HavreiKneset("Rina", "Reshima Meshutefet",false, false);
		HavreiKneset h3 = new HavreiKneset("Dorit", "kahol lavan",true, false);
		HavreiKneset h4 = new HavreiKneset("Ziv", "meretz",true, true);
		arr[0]=h1;
		arr[1]=h2;
		arr[2]=h3;
		arr[3]=h4;
		System.out.println(Arrays.toString(arr));
		System.out.println(isSuitable(arr,"meretz",true));
	
	}
}


What I have tried:

I tried to scan the array and check for any suitable elements
Posted
Updated 21-May-22 6:49am

Your recursive function is going too deep and running out of stack space.
Java
int index=0;
if(index>=arr.length) {
    return 1;
}

The value of index is always zero so your recursion will continue until stackoverflow, as you have seen.
 
Share this answer
 
v2
Comments
Member 15644190 21-May-22 12:14pm    
yeah I know, that's why I asked here how to solve it :)
Dave Kreskowiak 21-May-22 12:20pm    
Use the debugger. Step through the code line-by-line, inspecting the contents of variables after each line of execution to see what is happening and why.

The debugger is there not to debug the code, but to debug YOU and your understanding of the code.
Richard MacCutchan 21-May-22 12:32pm    
The value of index can never be greater than the length of the array because it is always zero. It needs to be passed in as a parameter so it will be incremented each call. Something like:
int recursiveMethod(int index, arr[] things)
{
    if (index >= things.length)
    {
        return 1;
    }
    else
    {
        int result = recursiveMethod(index + 1, things);
    }
}

// then you start with
public static void main(String[] args) {
    int result = recursiveMethod(0, myArray);
}
Member 15644190 21-May-22 12:21pm    
But in the else method i'm updating the index each time.
Richard MacCutchan 21-May-22 12:36pm    
No, you are updating the local variable which is then lost.
To add to what the others have said, when you declare a variable inside a method, it exists only until the code exits the method by reaching the closing curly bracket or a return statement. These are called local variables because they are "local" to the function and have no existence outside it.
To get it to work at all, you would been to pass the incremented value to the next iteration of the method as a parameter rather than initializing it in teh method at all.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900