This question already has answers here:
How to get an enum value from a string value in Java?

(29个答案)


已关闭6年。




选择Java备份并尝试使自己熟悉枚举类型。我正在尝试创建一个通讯录,用户可以在其中创建联系人。我已经创建了所有东西,但是挂了设置联系人类型(家庭, friend ,商务等)的电话。我已经在单独的Java类中设置了枚举类。
public class ContactType
{
    public enum contactType
    {
        Family,
        Church,
        Friend,
        BusninessColleague,
        ServicePerson,
        Customer,
        Other
    }
}

与我的联系人类看起来像:
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 = "[email protected]";
        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);中做错了什么,因为由于完全无知,我收到“Contacts类型中的setContactType(ContactType)方法不适用于参数(String)”,我不知道如何解决它将contactType设置为用户输入的任何内容。

最佳答案

您的问题是您调用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);

10-07 16:36