1
private ArrayList<SocialMediaAccount> socialMediaAccounts;

public void addSocialMediaAccount(String userID, String websiteName, String websiteURL, int activityLevel) 
{
    SocialMediaAccount object = new SocialMediaAccount(userID, websiteName, websiteURL, activityLevel);
    socialMediaAccounts.add(object);
}

I have this ArrayList and I need to search for a particular websiteName and return the userID associated with the object. Getting confused a lot and would like some help on getting started with this problem. Thanks!

1
  • Hi refer to the comment I left on your solution. Commented Apr 2, 2017 at 8:23

5 Answers 5

2

You can go through the arraylist and check if the websiteName matches the websiteName that is stored in the list like this:

for(int i = 0; i < socialMediaAccounts.size(); i++)
    if(socialMediaAccounts.get(i).getWebSiteName().equals(the_website_you_arelooking_for){
    return socialMediaAccounts.get(i).getUserId
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

if needs an extra right paren, but overall premise seems sound.
Hi, the header code I have to use is public String getSocialMediaID(String websiteName)
Hi no, I'm not too sure how to use that header with the code you provided or with Rahul's solution.
@FarazDurrani Any help is appreciated
1

If you have a lot of items in your list and you're doing a lot of searches, a HashMap using the the website name as the key will be much faster.

Comments

1

I suppose this method will solve your question. Hope you have getters inside your socialMediaAccount class.

public String getuserID(ArrayList<SocialMediaAccount> socialMediaAccounts,String websiteName){
 for(SocialMediaAccount s:socialMediaAccounts){  
    if( s.getWebsiteName().equalsIgnoreCase( websiteName)){
        return s.getUserID;
    } 
} 
return "no user found";
}

4 Comments

Rajith I think this answer is not correct. Because if the first condition fail, it will just return no user found and get out of the loop. don't you think?
@Faraz you are correct.. I have modified the code.
{} at end of if is suspect; shouldn't it be just {?
@KevinAnderson my bad. I fixed.
1

This should work.

//This method will return the userID associated with the given target websiteName.
//Insert it where you need it.
public String search(String targetWebsiteName){
    //loop through each account in your list.
    For(SocialMediaAccount acc: socialMediaAccounts){
      SocialMediaAccount tempObject = acc; 
       //check for websiteName
       if(tempObject.getWebsiteName().equals(targetWebsiteName)) return tempObject.getUserID();
    }
return null;
}


//Add these methods to your SocialMediaAccount class
class SocialMediaAccount{

       //Getters for object variables
       String getWebsiteName(){
            return websiteName;
       }

       String getUserID(){
           return userID;
       }
}

Comments

0
I think what you will want    

for (SocialMediaAccount socialMediaAccount: socialMediaAccounts) {
            if (socialMediaAccount.getwebsiteName() == your_website_search_name) {
                return socialMediaAccount.getId();
            }
        }
        return null; 

1 Comment

== won't do it; you need to use socialMediaAccount.getWebSiteName().equals(your_website_search_name)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.