This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problem Finding Index Value of a String in Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem Finding Index Value of a String in Array" Watch "Problem Finding Index Value of a String in Array" New topic

Problem Finding Index Value of a String in Array

Scott Kamen

Joined: Apr 01, 2003
Posts: 15
I'm working on a class method that will accept a string array and a string. It then loops to see if that string value matches any of the strings in the array and if it does then it returns the index value of the string in the array that it matches. If the string that is given to the method can't be found in the array then it's supposed to return a -1. This is a pat of a larger project as a whole but this is the part that's causing the problems. I keep getting told by the compiler that I need a return statement even though it appears that I have that covered. There is probably someway the exit the method w/ out going through the return statement which is why I'm getting the error but I just can't figure it out and it's been driving me crazy. Here is the code:

I could have something completley offball or wrong here but I just can't seem to figure it out.
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
the problem is that you don't have a return for every branch:
if () return i
if () return -1;
// you need a return right here
// or an plain else branch with a return
Quick fix solution: get rid of the "if (i==3)" part.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
The compiler is not quite smart enough to do the analysis that would be required to determine that your code is guaranteed to return. It sees

and thinks, well, if i != 3, this won't return, so the code isn't guaranteed to eventually returned. Yeah, we know that the loop will continue until eventually i == 3, but the compiler doesn't see that.
In fact your code is a bit more complex than necessary here. Try deleting the else {} clause entirely and study exactly what would happen when i == 3, and then i == 4. It's possible to add a single return statement in the appropriate location, that will solve your problem.
Incidentally, this whole method could also be coded as:

Don't worry about this if you haven't dealt with Collections yet - but it's worth delving into them eventually.

"I'm not back." - Bill Harding, Twister
Scott Kamen

Joined: Apr 01, 2003
Posts: 15
Thanks for the quick help but I'm still having some trouble. Phil , If I got rid of the (i==3) wouldn't it return a -1 after the first time through the loop that the strings don't match (which isn't what it's supposed to do)? Also , Jim , I tried your suggestion of getting rid of the entire else {} clause and attempting to think about what happens when i==3 and i==4 but I couldn't make any headway. The second way you proposed writing it looks tempting but It's a bit beyond what I'm doing right now (plus when I tried it in my code it gave me an error about the Arrays variable , would I have to do an import statement in order to get it to work?) so I think I'm going to stick with trying to get it to work with a loop.
thanks again,
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Isn't is a little silly to be checking for "i==3" inside of the 'for' loop?

And don't you need to make this just a little more generic?

Please ignore post, I have no idea what I am talking about.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

You're right that simply removing the if(i==3) line would return -1 on the first time through the loop. What you need is to have the return -1 outside the foor loop, as:

Also, this method will only tell you if the string is in the first three elements of the array. If the Array is longer than three elements (and the requested string is the fifth element), this method will never find it. And if the array has fewer than three element, you will get an ArrayIndexOutOfBounds exception. Try:

This will loop through the entire array, no matter how long it is.
And, yes, I agree with Jim that learning about Collection is well worth your time. To get Jim's solution to work, you need to import java.util.Arrays

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Scott Kamen

Joined: Apr 01, 2003
Posts: 15
Thanks a lot. I think at one point I basically had the right idea but I had the return -1 inside of the for loop which was what was causing the problems. The array I'm working w/ is a set wired 3 elements so that is never going to change so I can just use the number 3 but thanks for the help on suggesting a scalable method anyways.
I agree. Here's the link:
subject: Problem Finding Index Value of a String in Array
It's not a secret anymore!