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;
}
}
FacebookPersonfbUsers.contains(new FacebookPerson(name))fbUsers.contains(name)asnameis aStringbutfbUserscontainsFacebookPersonyou most likely would need to make your own method for this. Or do what @Fungucide said above