1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Need Java help please

  1. Apr 20, 2008 #1
    This is really making me mad. I keep getting a "this method must return a result of type int" error message, but obviously I have a return statement trying to return an int. This is a program that is supposed to perform a sequential search for a string index.



    public static int nameSearch(String[] inOrder, String searchedFor)
    {
    for (int i = 0; i < inOrder.length; i++)
    {
    if (searchedFor == inOrder)
    {
    return i;
    }
    else if (i > inOrder.length)
    {
    return -1;
    }
    }
    }
    Does anybody know what is wrong?
     
  2. jcsd
  3. Apr 21, 2008 #2

    exk

    User Avatar

    Your else if condition can't occur.

    The loop only runs while i < inOrder.legth and you are checking for i to be greater than your loop limit. If it doesn't find your search parameter it will return something that is not int and that's why you get the error.

    Something like this might work better:

    public static int nameSearch(String[] inOrder, String searchedFor)
    {
    for (int i = 0; i < inOrder.length; i++)
    {
    if (searchedFor == inOrder) break;
    if (i == inOrder.length - 1) i = -1;
    }
    return i;
    }
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Need Java help please
  1. Need help please (Replies: 1)

  2. Need java stack help (Replies: 3)

Loading...