Creating vector of user defined type


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Creating vector of user defined type

  1. #1
    Join Date
    Nov 2002
    Posts
    8

    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

  2. #2
    Join Date
    Oct 2002
    Posts
    66
    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
  •  
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