Results 1 to 4 of 4
  1. #1
    Vetements is offline Member
    Join Date
    Jan 2017
    Posts
    1
    Rep Power
    0

    Question Creating a find method to find objects in an array?

    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



    Java Code:
    public void changeABFF()
    	    {
    	        System.out.println("Enter first and the last name of the best friend you would like to change: ");
    	        String fname = keyboard.next();
    	        String lname = keyboard.next();
    	        BestFriends other = new BestFriends(fname,lname,"","");
    	        
    	        boolean found = false;
    	        for(int i=0;i<myBFFs.size();i++)
    	        {
    	        	if(other.equals(myBFFs.get(i)))
    	        	{
    	        		found = true;
    	    	        System.out.println("Enter a First Name: ");
    	    	        String fName = keyboard.next();
    	    	        System.out.println("Enter a Last Name: ");
    	    	        String lName = keyboard.next();
    	    	        System.out.println("Enter a Nick Name: ");
    	    	        String nName = keyboard.next();
    	    	        System.out.println("Enter a phone number");
    	    	        String cPhone = keyboard.next();	
    	    	        
    	    	        BestFriends tmp = myBFFs.get(i);
    	    	        tmp.firstName = fName;
    	    	        tmp.setLastname(lName);
    	    	        tmp.setNickName(nName);
    	    	        tmp.setCellPhone(cPhone);
    
    	        	}
    	        }
    	        
    	        if(found==false)
    	        {
        	        System.out.println("Sorry, I could not find your BFF to change it");
    	        }
                }
    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?

    Java Code:
    public int findBFF(BestFriends findBFF){
                    int i = 0;
                    boolean found = false;
                     
                    while(i < myBFFArray.length && found == false){
                        if(findBFF.equals(myBFFArray[i]))
                            found = true;
                        else
                            i++;
                        if (found == true)
                            return i;
                        else
                            return -1;
                    }
                    return i;
                }
    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;

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,991
    Rep Power
    10

    Default Re: Creating a find method to find objects in an array?

    But I keep getting a ton of errors whenever i try to put it under the changeABFF method.
    Can you be more specific? Are you getting compilations errors or runtime errors? Please describe what you are getting.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    298
    Rep Power
    3

    Default Re: Creating a find method to find objects in an array?

    The ArrayList only takes objects and now you are comparing objects (instead of Strings). The equals() method now returns true only if two references are pointing to the same object, which they will never do since you are creating a new object from the input in line 6. IMHO instruction #1 says "override the equals() method".

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,991
    Rep Power
    10

    Default Re: Creating a find method to find objects in an array?

    And I didn't say this earlier but your second bit of code will not search the list.
    Lines 6 thru 9 will always execute regardless of what happened before. So you
    will either return i or -1 immediately.

    Java Code:
    while(i < myBFFArray.length && found == false){
       if(findBFF.equals(myBFFArray[i]))
          found = true;
       else
           i++;
       if (found == true)
           return i;
       else
           return -1;
    }
    And always use {}. Even for one liners. So

    Java Code:
    if (found) {
      // one line
    } else {
      // one line
    }
    It will save you in the long run.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Method to find sum split even point in array
    By uli in forum New To Java
    Replies: 0
    Last Post: 01-31-2013, 07:57 PM
  2. Creating an Array of Objects Method
    By DBaskov in forum New To Java
    Replies: 14
    Last Post: 07-18-2011, 01:33 PM
  3. Replies: 1
    Last Post: 02-16-2008, 10:10 PM
  4. Replies: 2
    Last Post: 02-16-2008, 09:52 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
  •