根据用户输入设置枚举
Setting an enum from user input
选择 java 备份并尝试熟悉枚举类型。我正在尝试创建一个地址簿,用户可以在其中创建联系人。我已经创建了所有内容,但挂断了设置联系人类型(家庭、朋友、企业等...)我在单独的 java [= 中设置了一个枚举 class 25=]。
public class ContactType
{
public enum contactType
{
Family,
Church,
Friend,
BusninessColleague,
ServicePerson,
Customer,
Other
}
}
我的联系人 class 看起来像:
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);
因为我完全不知道 "The method setContactType(ContactType) in the type Contacts is not applicable for the arguments (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);
调用 ContactType.valueOf(strContactType) 将字符串名称转换为枚举值
选择 java 备份并尝试熟悉枚举类型。我正在尝试创建一个地址簿,用户可以在其中创建联系人。我已经创建了所有内容,但挂断了设置联系人类型(家庭、朋友、企业等...)我在单独的 java [= 中设置了一个枚举 class 25=]。
public class ContactType
{
public enum contactType
{
Family,
Church,
Friend,
BusninessColleague,
ServicePerson,
Customer,
Other
}
}
我的联系人 class 看起来像:
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);
因为我完全不知道 "The method setContactType(ContactType) in the type Contacts is not applicable for the arguments (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);
调用 ContactType.valueOf(strContactType) 将字符串名称转换为枚举值