Setting an enum from user input
enums, java
Solution
Your problem is that you call `valueOf(...)` but you discard the ContactType object that is returned by this method call.... i.e., you never assign the returned object to a variable.
First get rid of your unnecessary wrapper by changing this:
public class ContactType
{
public enum contactType
{
Family,
Church,
Friend,
BusninessColleague,
ServicePerson,
Customer,
Other
}
}
to this:
public enum ContactType
{
Family,
Church,
Friend,
BusninessColleague,
ServicePerson,
Customer,
Other
}
Next use the valueOf to set a ContactType variable.
strContactType = JOptionPane.showInputDialog ("Please enter contact type (Family, Church, BusinessColleague, ServicePerson, Customer, or Other)");
ContactType contactType = ContactType.valueOf(strContactType);
JOptionPane.showMessageDialog (null, strContactType);
c1.setContactType (contactType);
Note, I would try to make it more idiot-proof by limiting the selections to a ContactType. For example:
Object selection = JOptionPane.showInputDialog(null,
"Choose a contact type", "Contact Type",
JOptionPane.INFORMATION_MESSAGE, null,
ContactType.values(), ContactType.values()[0]);
// check that selection is not null before using
System.out.println(selection);
Problem
Picking java back up and trying to familiarize myself with enum types. I'm trying to create an address book where user can create contacts. I've created everything already, but am hung up on setting a contact type (Family, Friends, Business, etc...) I've set up an enum class in a separate java class. ``` public class ContactType { public enum contactType { Family, Church, Friend, BusninessColleague, ServicePerson, Customer, Other } } ``` With my Contacts class looking like: ``` public class Contacts { private contactType contact; private String name; private String streetAddress; private String city; private String state; private String zipCode; private String phone; private String email; private String photo; public Contacts ( ) { contact = null; name = "XXX XXX"; streetAddress = "XXX"; state = "XX"; zipCode = "00000"; phone = "XXX-XXXX"; email = "XXX@XXX.COM"; photo = "XXX.jpg"; } public Contacts (ContactType contactType, String name, String streetAddress, String city, String state, String zipCode, String phone, String email, String photo) { this.contact = contactType; this.name = name; this.streetAddress = streetAddress; this.city = city; this.state = state; this.zipCode = zipCode; this.phone = phone; this.email = email; this.photo = photo; } public ContactType getContactType ( ) { return contact; } public void setContactType (ContactType input) { this.contact = input; } //rest of code ``` and finally my driver, it includes a menu (everything else works except for setting the contact type so i've just included that snippet for the sake of keeping it short): ``` switch (iSelection) { case 1: c1 = new Contacts(); //creates a new contact break; case 2: strContactType = JOptionPane.showInputDialog ("Please enter contact type (Family, Church, BusinessColleague, ServicePerson, Customer, or Other)"); contactType.valueOf(strContactType); JOptionPane.showMessageDialog (null, strContactType); c1.setContactType (strContactType); break; ``` I know i've done something wrong here `c1.setContactType(strContactType);` because i'm getting "The method setContactType(ContactType) in the type Contacts is not applicable for the arguments (String)" due to complete ignorance I don't know how to fix it to set the contactType to whatever the user inputs.