The below is the main method, the wrapper method, and the array search method for a step recursive method used for finding the index position of the searched string.

I am encoutering this problem when whatever search I put in, I get the return value of -1.
Why and how is this happening?

Thanks!

public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub


		int n=0, i; //n will store the user input for the number of integers
						   //it is set to its default value 0
				  			//i will serve the purpose of a counter
		String search="";
		String[] array; //creating a double array


		//The line below creates a reference to a new Scanner object
		// called "sc". It reads lines from standard input one at a time.
		Scanner sc = new Scanner (System.in);

		boolean checkException = false;//creates a boolean variable checkException
									   //and sets it to its default value false

		do {
			System.out.print ("How many strings would you like to enter? ");
			try {
				n = sc.nextInt();	// stores the next integer typed by the user in n using Scanner sc
				checkException = true;//sets the boolean value to true, will cause
									  //the do-while loop to exit
			}
			catch (InputMismatchException e)//catch values that are not integers
			{
				System.out.println("This is not a correct integer, please try again.");//error message
				sc.next(); //clears Scanner sc for next input
				checkException = false;//sets boolean value to false, continues the loop
			}
			catch(ArrayIndexOutOfBoundsException a)//catches an array that has been accessed with an
												   //illegal index, either negative or greater than or
												   //equal to the size of the array
			{
				System.out.println("This is not a correct integer, please try again.");
				sc.next();//clears Scanner sc for further input
				checkException = false;
			}
			array = new String[n]; //declares the array variable array, giving it n elements


		} while (checkException == false); //remains in loop as long as the boolean variable is false

		checkException = false;

		for (i=0; i<array.length; i++)
		{
			do {
				System.out.println("Please enter your strings:");
				try {
					array[i]= sc.next();//stores the user inputs into the indexed array elements
					checkException = true;
				}
				catch (InputMismatchException e)//catches non-integer values and display the error message
				{
					System.out.println("This is not a correct string, please try again.");
					sc.next();
				}
			} while (checkException == false);
		}
		do {
			System.out.println("Please enter your search:");
			try {
				search = sc.next();//stores the user inputs into the indexed array elements
				checkException = true;
			}
			catch (InputMismatchException e)//catches non-integer values and display the error message
			{
				System.out.println("This is not a correct string, please try again.");
				sc.next();
			}
		} while (checkException == false);

		findString(array, search);

	}
	public static void findString (String [] array, String search) {

		System.out.print(findString(array, 0, search));

		}

	public static int findString (String []array, int head, String search){

		if (head== array.length)
			return -1;

		if (array[head]==search)
			return head;


		return findString (array, head+1, search);
	}

}

I once encountered the same problem using this if condition

if (array[head]==search)

try coding it this way

if (array[head].equals(search)) {

and see what happens.

Hope this helps (",).

I once encountered the same problem using this if condition

if (array[head]==search)

try coding it this way

if (array[head].equals(search)) {

and see what happens.

Hope this helps (",).

Thanks, I haven't thought of that, even though the problem always occurs to me. :sad: it works right now!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.