Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with modifying an assignment.

 
Adam Gomez
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic