- #1
csgirl504
- 18
- 0
Homework Statement
I'm working on a program that uses an ArrayList to hold a collection of UserID objects (which contains the first and last names, plus username and password).
When I add a new UserID to the list, I want to check to make sure that it is not already there.
This is the error I am getting, and my code follows.
UserList.java:36: error: unexpected type
if (userList.indexOf(user) = -1)
^
required: variable
found: value
Homework Equations
Code:
import java.util.ArrayList;
public class UserList
{
private ArrayList<UserID> userList; /**
* Constructor to create an ArrayList to store a collection of UserID objects
*/
public UserList ()
{
userList = new ArrayList<UserID>();
}
/**
* Method to add a new UserID to the collection.
* Returns true if a user with the same ID is not already in the collection.
* Returns false if the user already exists.
*
* @param user the UserID object to be added
* @return true or false
*/
public boolean addUser(UserID user)
{
if (userList.indexOf(user) = -1)
return false;
else
{
userList.add(user);
return true;
}
}
}
The Attempt at a Solution
I am using the "userList.indexOf(user) = -1 " to check to see if the object is already there. Why is it telling me that user is an unexpected type when my ArrayList is a collection of UserID objects, and user is a UserID object? I'm just learning ArrayLists and am very confused!