从用户输入设置枚举

时间:2015-02-24 19:30:55

标签: java enums

挑选java备份并尝试熟悉枚举类型。我正在尝试创建一个用户可以创建联系人的地址簿。我已经创建了所有内容,但我已经挂断了设置联系人类型(家人,朋友,商务等等)。我在一个单独的java类中设置了一个枚举类。

public class ContactType
{
    public enum contactType
    {
        Family,
        Church,
        Friend,
        BusninessColleague,
        ServicePerson,
        Customer,
        Other
    }
}

我的Contacts类看起来像:

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

最后是我的驱动程序,它包含一个菜单(除了设置联系人类型之外的所有其他工作,所以我只是为了保持简短而包含该代码段):

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;

我知道我在这里做错了c1.setContactType(strContactType);,因为我得到了“联系人类型中的方法setContactType(ContactType)不适用于参数(String)”由于完全无知我不知道知道如何修复它以将contactType设置为用户输入的任何内容。

2 个答案:

答案 0 :(得分:3)

您的问题是您调用了valueOf(...)但是丢弃了此方法调用返回的ContactType对象....即,您永远不会将返回的对象分配给变量。

首先通过更改此内容来删除不必要的包装:

public class ContactType
{
    public enum contactType
    {
        Family,
        Church,
        Friend,
        BusninessColleague,
        ServicePerson,
        Customer,
        Other
    }
}

到此:

    public enum ContactType
    {
        Family,
        Church,
        Friend,
        BusninessColleague,
        ServicePerson,
        Customer,
        Other
    }

接下来使用valueOf设置ContactType变量。

      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);

注意,我会尝试通过将选择限制为ContactType来使其更加白痴。例如:

  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);

答案 1 :(得分:-1)

调用ContactType.valueOf(strContactType)将字符串名称转换为枚举值