Find Last Element | One Test Case Failing

I have written the code, but one test case is failing. Can you help to identify which case is that?? I have checked for -ve numbers also.

import java.util.Scanner;

public class FindLastElement {

public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	String sizeofArray = sc.nextLine();
	String elementsofArray = sc.nextLine();
	String elementToSearch = sc.nextLine();
	int[] array = new int[Integer.parseInt(sizeofArray)];
	String[] result = elementsofArray.split("\\s");
	for (int x=0; x<result.length; x++)
     array[x] = Integer.parseInt(result[x]);
 
 
	System.out.println(findLastElement(array,Integer.parseInt(elementToSearch),0));

}

public static int findLastElement(int[] array, int numberToSearch, int selectedIndex){
	
	int result = -1;
	
	if(selectedIndex == array.length-1 ){
		if ( array[selectedIndex] == numberToSearch )
			return selectedIndex;
		else
			return result;
	}
		

	for( int x=selectedIndex; x<array.length-1; ++x){
		
		if( array[selectedIndex] == numberToSearch)
			result = selectedIndex;

		int currentresult = findLastElement(array,numberToSearch,x+1);
		if( currentresult != -1 )
		{
			result = currentresult;
		}
		
	}
	
	return result;
}

}

Hey @connectrajanjain The test case you are failing here is :
5
6 9 3 9 0
9
As deeply observing the problem here is that after 5 in the first line there is a space. So if you try to take input using string and then convert the string to int that will give an error bcoz 5 plus a space is not an integer. So take input directly in the form of Int not in string.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.