Reputation: 1
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;
}
}
Upvotes: 0
Views: 105
Reputation: 53
You could use a 2d array list to store the information and link it to the name. That would fix your problem
Upvotes: 1
Reputation: 4692
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.
Upvotes: 0
Reputation: 532
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;
}
}
Upvotes: 2