• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a find method to find objects in an array?  RSS feed

 
Leon Tee
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, first post here.

I'm trying to create methods to find objects in an array l in order to both change and remove them, as we are changing the program from an array list to an array. Here is the current code





I followed the pseudocode and came up with this. But I keep getting a ton of errors whenever i try to put it under the changeABFF method. Im so lost on this, can anyone point me in the right direction?




the instructions were:

1.) equals method must be already defined
2.) In change method, as well as the remove method, you are going to need to create a
int FIND method (BestFriend findBFF):
a. Find index = 0; found = false
b. While loop (find index < myBFFArray.length && found == false)
If (findBFF.equals(myBFFArray[Find index])
     Found = true;
Else
     Find index ++;
c. If (found == true)
       return Find index;
  Else
        Return -1;
3.) After calling the FIND method, if result was -1, display “Sorry, cannot change/remove your friend’s contact info. They don’t exist” 

4.) Otherwise for a CHANGE, use the setters to change the object found at the index position returned by the FIND method:
findBFF.setFirstName(whatever);
findBFF.setLastName(whatever);
…..all the setters…
myBFFArray[index of found] = findBFF; 

5.) Otherwise for a REMOVE,
myBFFArray[index of found] = null;  



 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leon Tee wrote:I followed the pseudocode and came up with this. But I keep getting a ton of errors whenever i try to put it under the changeABFF method. Im so lost on this, can anyone point me in the right direction?

Your changeABFF() method as shown is missing a closing '}'. Could you cut and paste the error message into a new post.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Inside your loop you will either return on line 11 or on line 13, thus preventing the loop from ever going around again. Remove lines 12 & 13.

Even with this fix I find this code pretty ugly. You've got a loop and you want to return the index when/if found, else return -1.

 
Leon Tee
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Inside your loop you will either return on line 11 or on line 13, thus preventing the loop from ever going around again. Remove lines 12 & 13.

Even with this fix I find this code pretty ugly. You've got a loop and you want to return the index when/if found, else return -1.



thank you, this was bugging the hell out of me. the second code you posted is the cleaned up version?
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

Also, never do
always
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Remove lines 12 & 13...

This is only one of the problems. Still wouldn't return -1.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:. . . never do
. . .
Not even if it appears in your instructions. That construct is poor style and error‑prone if you write = instead of ==.

And welcome to the Ranch
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!