Results 1 to 4 of 4
Thread: String to an object?
- 10-27-2009, 03:34 PM #1
Member
- Join Date
- Oct 2009
- Location
- Palm Springs
- Posts
- 3
- Rep Power
- 0
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! :oLast edited by bobbyboyy; 10-27-2009 at 03:38 PM.
- 10-27-2009, 03:41 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
You have to make a new Email object and set it's (String) email member; something like this:
imho you should also create another c'tor in your Email class, one that takes a String argument.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 }
kind regards,
Jos
- 10-27-2009, 05:07 PM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
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!!!
- 10-27-2009, 07:43 PM #4
Member
- Join Date
- Oct 2009
- Location
- Palm Springs
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Object name by string variable?
By zerkz in forum New To JavaReplies: 4Last Post: 10-14-2009, 07:16 AM -
Object to String.
By Gelembjuk in forum New To JavaReplies: 6Last Post: 10-29-2008, 10:19 PM -
String to Object conversion
By moaxjlou in forum New To JavaReplies: 1Last Post: 10-29-2008, 01:04 AM -
Object from String
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 09:20 PM -
String vs Object
By Gilgamesh in forum New To JavaReplies: 1Last Post: 11-28-2007, 10:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks