Results 1 to 4 of 4
  1. #1
    bobbyboyy is offline Member
    Join Date
    Oct 2009
    Location
    Palm Springs
    Posts
    3
    Rep Power
    0

    Smile String to an object?

    Hello, I am very new to Java so please forgive me!
    I am doing some tutorials on associating classes. I have 2 classes; 1 called User, the other called Email. A 'user' has an 'email' address.

    I need to do a setEmail method in the User class, but I am having a problem. Here is a bit of my code...

    My User class...

    public abstract class User {
    private Email email;
    private String name;
    private String userId;

    public User() {
    name = "";
    userId = "";
    email = null;
    }

    public void setEmail(String email) {
    this.email = email;
    }





    My Email class:


    public class Email {

    private String email;

    public Email() {
    email = "";
    }

    public void setEmail(String email) {
    this.email = email;
    }

    public String getEmail() {
    return email;
    }






    In setEmail (in the User class) I want to use the string parameter to create a new 'Email' object and place it in the email variable. How can I do this?

    I am getting an incompatible types error... because the Email is an object and the email variable is a string?

    Please any help is much appreciated,
    thankyou! :o
    Last edited by bobbyboyy; 10-27-2009 at 03:38 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    You have to make a new Email object and set it's (String) email member; something like this:

    Java Code:
    public void setEmail(String email) {
       Email e= new Email(); // create a new Email object;
       e.setEmail(email); // set its email address
       this.email= e; // set the Email object in this class
    }
    imho you should also create another c'tor in your Email class, one that takes a String argument.

    kind regards,

    Jos

  3. #3
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    Hello,

    JosAH is Right...

    You might could also decide to Change the parameter the setEmail() takes to Email, That's why it's giving you an incompatible type.

    All the best!!!

  4. #4
    bobbyboyy is offline Member
    Join Date
    Oct 2009
    Location
    Palm Springs
    Posts
    3
    Rep Power
    0

    Talking Thankyou!!

    Thankyou very much for your quick replies!!

    I am just starting with Java and you've helped me alot,

    thankyou again! :D :D

Similar Threads

  1. Object name by string variable?
    By zerkz in forum New To Java
    Replies: 4
    Last Post: 10-14-2009, 07:16 AM
  2. Object to String.
    By Gelembjuk in forum New To Java
    Replies: 6
    Last Post: 10-29-2008, 10:19 PM
  3. String to Object conversion
    By moaxjlou in forum New To Java
    Replies: 1
    Last Post: 10-29-2008, 01:04 AM
  4. Object from String
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 09:20 PM
  5. String vs Object
    By Gilgamesh in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 10:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •