• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst
  • Peter Rooke
  • Mikalai Zaikin
Bartenders:
  • Himai Minh

Problem Finding Index Value of a String in Array

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
thanks,
-Scott
 
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the problem is that you don't have a return for every branch:
if () return i
else
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Scott Kamen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
-Scott
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Scott Kamen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
-Scott
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic