【问题标题】:Setting an enum from user input [duplicate]从用户输入设置枚举[重复]
【发布时间】:2015-04-26 14:17:12
【问题描述】:

选择 java 备份并尝试熟悉枚举类型。我正在尝试创建一个通讯簿,用户可以在其中创建联系人。我已经创建了所有内容,但是我一直在设置联系人类型(家庭、朋友、商务等)我已经在一个单独的 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 = "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

最后是我的驱动程序,它包括一个菜单(除了设置联系人类型外,其他所有功能都有效,所以为了简短起见,我只包含了该 sn-p):

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 设置为用户输入的任何内容。

【问题讨论】:

  • 我已经删除了您的javascript 标签,因为您的问题与使用这种语言无关。请理解这是两种完全不同的编程语言,就像 ham 和 hamburger 密切相关,如果你错误地标记了你的问题,你将无法让合适的专家来审查它,这可能会损害你获得答案的机会体面的帮助。

标签: java enums


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • ContactType.valueOf(...),正如 OP 所写,在哪里存在?
    • 当然是在 ContactType.contactType 中
    • 好的...但是当您查看问题时,很明显他们对此感到困惑,因为他们有一个 contactType 成员,他们试图为其分配一个 ContactType 值。我认为可能值得编辑以澄清他们的整体问题。
    猜你喜欢
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多