0

I am having trouble with looking through my ArrayList of FacebookPerson object for a string (name). This is my first project working with ArrayLists, and as such I have, at best, only a basic idea of what I'm doing. What I'm not understanding is why fbUsers.contains(name) does not seem to do anything, and how I can correctly search for a string name.

---testFacebook_ArrayList.java

package Facebook;

import java.util.*;

public class testFacebook_ArrayList {
    public static Scanner Input = new Scanner(System.in);

    public static void main(String[] args){
        String name, mood;
        boolean exit = false;
        ArrayList<FacebookPerson> fbUsers = new ArrayList<FacebookPerson>();

        while(!exit){
        System.out.print("Enter the name of a facebook user to be created (enter #### to end creation & move to user selection): ");
        name = Input.next();
        Input.nextLine();

            if(name.equals("####")){
            exit = true;
            }
            else if(fbUsers.contains(new FacebookPerson(name))){
                System.out.println("Error, name already exists. Try again.");
                continue;
            }
            else{
                fbUsers.add(new FacebookPerson(name));
            }
        }
        exit = false;
        while (!exit){
            System.out.print("Enter a user's name to modify their mood (#### to terminate the program): ");
            name = Input.nextLine();

            if (name.equals("####")){
                System.out.println("Program terminated.");
                System.exit(1);
            }
            else if (fbUsers.contains(new FacebookPerson(name))){
                System.out.print("Enter a mood for " + name + ": ");
                mood = Input.nextLine();
                for (int i = 0; i < fbUsers.size(); i++){
                    if(fbUsers.get(i).equals(new FacebookPerson(name))){
                        fbUsers.get(i).setMood(mood);
                    }
                }
            }
            else{
                System.out.println("Unrecognized name. Try again.");
                continue;
            }
        }
    }
}

---FacebookPerson.java

  // This is the FacebookPerson_Graphics class
  // Written by Dr. Xiaolin Hu
  // 03/05/2015
  package Facebook;

  public class FacebookPerson{

  private String myName;
  protected String myMood;
  protected Facebook myfacebook;

  public FacebookPerson(String name){
      myName = name;
      myfacebook = new Facebook(myName);
      //System.out.println("FacebookPerson's constructor");
  }

  public FacebookPerson(){

  }

  public String getName(){
      return myName;
  }

  public void setMood(String newMood){
     myMood = newMood;
     myfacebook.setContent(myMood);
  }

  public String getMood(){
      return myMood;
  }

}

---Facebook.java

// This is the Facebook class
// Wrriten by Dr. Xiaolin Hu
// 03/05/2015
package Facebook;

import java.awt.*;

public class Facebook{

   private String name;
   private String content;
   DrawingPanel panel;
   private Graphics g;

   public Facebook(String nm){
       content = "undefined";
       name = nm;

       // Create the drawing panel
       panel = new DrawingPanel(200, 150);
       g = panel.getGraphics();

       // display name
       g.drawString(name+"'s mood is undefined.", 20, 75);
   }

   public void setContent(String newContent){
    content = newContent;
        if(content.equals("happy")){
            g.setColor(Color.red);
            g.fillRect(0, 0, 200, 150);
            g.setColor(Color.black);
            // display mood
            g.drawString(name+"'s mood is:"+ "happy", 20, 75);
        }
        else{
            g.setColor(Color.white);
            g.fillRect(0, 0, 200, 150);
            g.setColor(Color.black);
            g.drawString(name+"'s mood is:"+ content, 20, 75);
        }
   }

   public String getContent(){
    return content;
   }
}
9
  • add code for FacebookPerson Commented Nov 13, 2015 at 5:20
  • 1
    change it to fbUsers.contains(new FacebookPerson(name)) Commented Nov 13, 2015 at 5:22
  • 2
    I'm assuming because you are doing fbUsers.contains(name) as name is a String but fbUsers contains FacebookPerson you most likely would need to make your own method for this. Or do what @Fungucide said above Commented Nov 13, 2015 at 5:23
  • 1
    Unfortunately Fungucide's method did not work. It seems to ignore it completely still. Commented Nov 13, 2015 at 5:25
  • 1
    @Calderious are you sure.. Commented Nov 13, 2015 at 5:30

3 Answers 3

2

You could do a for loop for every user and call getName();

for (FacebookPerson fbp : fbUsers ){
     if (fbp.getName().equals(name)){
          System.out.println("Error, name already exists. Try again.");
          continue;
     }
}
Sign up to request clarification or add additional context in comments.

6 Comments

For some reason, this still does not work for me. Even with this comparison, it still seems to be ignored.
Please just post the whole code so I can help you debug
Done. Though, you should know that there are some other issues with my code as well not relating to this quesiton.
@Fungucide you do not compare String like that.
@Fungucide please see the if condition in your alternative solution
|
1

You could use a 2d array list to store the information and link it to the name. That would fix your problem

Comments

0

Calderious,

You need to override and implement FacebookPerson's equals(...) method in order for contains to work. If you look at the List's contains(...) javadoc, it specifically mentions:

... returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).

add the following in your class

public class FacebookPerson {
  ...

  @Override
  public boolean equals(Object obj) {
    // your equals logic goes here
  }

  ...
}

or even better, IDEs can now create this based on your declared attributes. In Eclipse, you just need to right click on the code > Source > Generate hashCode() and equals().

I hope that helps.

Comments

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.