Results 1 to 4 of 4
Thread: help writing array method
- 10-16-2011, 05:17 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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
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.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
And will the break cause it to stop after it finds the first(and the smallest) index?
Any help would be greatly appreciated.
-
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.
- 10-16-2011, 05:29 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
Re: help writing array method
Is this any better?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
-
Re: help writing array method
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 05:35 PM.
Similar Threads
-
Writing a method to write a method?
By balla in forum New To JavaReplies: 5Last Post: 04-01-2011, 12:05 PM -
Method Writing Help
By socboy6579 in forum New To JavaReplies: 5Last Post: 11-16-2010, 09:14 PM -
Writing a method
By Proton in forum New To JavaReplies: 10Last Post: 05-13-2010, 02:03 PM -
I need some help in writing a method
By loraineEd in forum New To JavaReplies: 2Last Post: 01-02-2010, 04:23 PM -
Writing the filter method
By ai_2007 in forum Advanced JavaReplies: 1Last Post: 07-03-2007, 03:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks