-
Creating vector of user defined type
Hi,
I'm having some trouble creating a vector of a user defined type. Here's my user defined type:
public class User {
// variables that define a user
protected String userType;
protected String userLogin;
protected String userPassword;
// constructor
public User(String type, String login, String password)
{
userType = type;
userLogin = login;
userPassword = password;
}
// get the userLogin
public String getLogin()
{
return userLogin;
}
} // end class User
Fairly simple. Now I'm using this class to create users of different types for a program. When they login to the program. It asserts that their login and password are valid and depending on their type displays different options. My problem is extracting an element from the vector and getting to its variables. The following is excerps from my file where I try to check the login field.
Here's how I declared everything:
private User user1, user2, user3;
private Vector dummy;
user1 = new User("supervisor", "Carla", "Hopkins");
user2 = new User("child", "Riesa", "de Beer");
user3 = new User("child", "Lucas", "Cockerham");
dummy = new Vector(3);
dummy.addElement(user1);
dummy.addElement(user2);
dummy.addElement(user3);
I have created a class to handle a button event that will
check to see if the login entered matches a login in the vector of Users
String loginKey = loginText.getText();
// determine if dummy vector contains the login
int size = dummy.size();
User loginCheck;
for(int i = 0; i < size; i++)
{
loginCheck = dummy.get(i);
if (loginCheck.getLogin() == loginKey)
statusText.setText("Login valid: " + loginKey);
else
statusText.setText("Login not valid: " + loginKey);
}
I keep getting the following error:
incompatible types
found: java.lang.Object
required: User
loginCheck = dummy.get(i);
What am I doing wrong? Thanks for your time and input.
Riesa
-
Sorry, I've not had time to read it all but skimmed it over and can see one error (repeated a few times).
You are doing:
loginCheck = dummy.get(i);
Vector takes in input of an Object. Whenever you have something that takes an input of an Object you need to cast it when extracting. Therefore you should change the above to:
loginCheck = (User) dummy.get(i);
Hope that helps!
Meethoss
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks