Results 1 to 4 of 4
  1. #1
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default help writing array method

    Create a method called findElementIndex, which takes an integer and an integer array as input parameters, and returns the smallest index where that value appears. If the value is not in the array, the method should return -1.
    Example:
    value: 3 theArray:{10, 3, 6, 3, 8, 10} ==> 1
    value:10 theArray:{11, 3, 6, 7, 9, 60} ==> -1

    Java Code:
    public int findElementIndex(int value, int[] theArray){
      for(int j=0; j<theArray.length;j++)
        if (theArray[j]==value) {int result=theArray[j];break;return result;}
      else
      System.out.print(-1);
    
    }// end method
    I can't figure out where the return statement would go...I know its in the wrong spot right now, but I can't figure out where to put it. I tried putting it after the else statement put that didnt work either.
    And will the break cause it to stop after it finds the first(and the smallest) index?
    Any help would be greatly appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: help writing array method

    Suggestions:
    • Your return statement is in the correct location.
    • But that's not enough. Your program can potentially not return anything. For instance, if the value is not found, what is returned?
    • Note that it is OK to have more than one return statement in a method.
    • To answer that question above, you need to understand that System.out.print or System.out.println doesn't return anything. Should you even have that line in your program?
    • For your sanity and for ours, be a little more generous with your use of white space. Put each line of code on its own line. You gain nothing by trying to save space putting that if block in one line and you lose a lot in readability.
    • Also enclose all code blocks in curly braces. Above that means that the code block for your for loop should be enclosed, even if it is just one if statement.

  3. #3
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: help writing array method

    Java Code:
    public int findElementIndex(int value, int[] theArray)
    {
      for(int j=0; j<theArray.length;j++)
        if (theArray[j]==value) {
        int result=theArray[j];
        return result;
      }
      else{
        int outcome=-1; 
        return outcome;
      }
     
    
    }// end method
    Is this any better?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: help writing array method

    Quote Originally Posted by katiebear128 View Post
    Java Code:
    public int findElementIndex(int value, int[] theArray)
    {
      for(int j=0; j<theArray.length;j++)
        if (theArray[j]==value) {
        int result=theArray[j];
        return result;
      }
      else{
        int outcome=-1; 
        return outcome;
      }
     
    
    }// end method
    Is this any better?
    No, not quite. You still need to enclose your for loop in curly braces. I would also get rid of the else block and place my default return at the end of the method after the for loop. Can you think through the logic and see why I recommend this? ;)

    The bottom line is to always compile and test your code, usually before posting here. Had you done this, you'd see immediately that while it compiles, it's logic is off.
    Last edited by Fubarable; 10-16-2011 at 06:35 PM.

Similar Threads

  1. Writing a method to write a method?
    By balla in forum New To Java
    Replies: 5
    Last Post: 04-01-2011, 01:05 PM
  2. Method Writing Help
    By socboy6579 in forum New To Java
    Replies: 5
    Last Post: 11-16-2010, 10:14 PM
  3. Writing a method
    By Proton in forum New To Java
    Replies: 10
    Last Post: 05-13-2010, 03:03 PM
  4. I need some help in writing a method
    By loraineEd in forum New To Java
    Replies: 2
    Last Post: 01-02-2010, 05:23 PM
  5. Writing the filter method
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-03-2007, 04:32 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •