We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,124 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help with Step Recursive array search

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);
	}

}
2
Contributors
2
Replies
2 Hours
Discussion Span
2 Years Ago
Last Updated
3
Views
Question
Answered
flyingcurry
Light Poster
44 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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 (",).

Eric Cute
Posting Whiz in Training
252 posts since Jun 2010
Reputation Points: 55
Solved Threads: 20
Skill Endorsements: 2

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!

flyingcurry
Light Poster
44 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 2 Years Ago by Eric Cute

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0648 seconds using 2.68MB