Problem creating a vector of a user defined type


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Problem creating a vector of a user defined type

  1. #1
    Join Date
    Nov 2002
    Posts
    8

    Problem creating a vector of a 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 alid 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

  2. #2
    Join Date
    Nov 2002
    Posts
    2
    Riesa -

    Vector.get() returns Object. Now you and I know that all you put into that Vector were Users, but the get() method only returns Object. For your assignment to work, you will need to cast the result as follows:

    loginCheck = (User)dummy.get(i);

    You might also want to look at the proposed addition to Java to support "generics" (similar in concept to templates in C++, but without the code bloat baggage) that would allow you to define a Vector containing objects of a particular type, and would allow you to get() those objects out without casting them (and would also verify that addElement args were of the proper type, too).

    -- Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center