This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help with modifying an assignment. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with modifying an assignment." Watch "Help with modifying an assignment." New topic
Author

Help with modifying an assignment.

Adam Gomez
Greenhorn

Joined: Sep 10, 2012
Posts: 1
Hey all. Sorry to make something like this my first post, but I can't for the life of me figure out what exactly I'm supposed to change for my assignment. The goal of the first part of the homework is to make the array continue to print after the key number is found in the array. At the moment it stops after the number entered is found. I've tried messing with the return in the search part of the program, but nothing seems to get the array to continue printing. Any advice would be greatly appreciated.

jatan bhavsar
Ranch Hand

Joined: Jul 23, 2008
Posts: 296

Hi Adam,

array continue to print after the key number is found in the array

Can you please explain this?

if you would like return the key you can do it once all elements are printed.you can store key in temporary variable and return it from method once the array finish the printing.
Regards
jatan
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Adam Gomez wrote:...The goal of the first part of the homework is to make the array continue to print after the key number is found in the array. At the moment it stops after the number entered is found.

Your code stops printing after the number entered is found, because as soon as you find element in the array you return it from your method. A good way to accomplish what you want is the one proposed by jatan, but with slight modification. There is no need to keep a temp variable to return the value you are searching from your search method (you have a value you are searching as argument to method call). I would rather keep the index (position) of the element found if it exists in an array, and -1 otherwise. After you complete with for loop, just return that temp value, and by knowing the index of the value you can access it any time.


The quieter you are, the more you are able to hear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with modifying an assignment.
 
Similar Threads
Recursion
Card Shuffle Problem
Timing Performance of a Sequential Search
Generic Binary Search for Ints & Strings
Sequential Search Performance