1

I have a enum for gender.

public enum Gender {
  Male("M"), Female("F");
  private String value;

  Gender(String value){
      this.value = value;
  }


  public String getValue() {
      return value;
 }
}

This enum is a constructor of my class.

class People {
   private String name;
   private int id;
   private int age;
   private Gender x;
}

Then I'm trying to create a new object of this class from the user, a user type a name, id, age and gender. I'm using the dialog box JOptionPane. The line I'm getting a error is this one.

 public class AppPeople {
 public static void main(String[] args) {
   Gender gender1; //I tried declaring String gender1
                   //To get the answer/input below, but didn't work.
   gender1 = JOptionPane.showInputDialog(null, "Type Male or Female");
   People p1 = new People(name, id, age, Gender1);
   }
}

All the others fields are working from the dialog box, name, id and age. This one that is a enum isn't working. I had to declare id and age string to use it on dialog box, so the answer typed I got into strings variables and converted it to integer, to match the constructor of the class. I tried declaring a new string to get the input from dialog box and convert it to enum but still didn't work. The only field left is this one to convert a string to enum. Does anyone know what can I do to fix or maybe a new solution.

RonanFelipe
  • 590
  • 1
  • 9
  • 23

2 Answers2

0

just se method of each enum

Gender.valueOf("yourString")
user1722245
  • 2,065
  • 1
  • 18
  • 31
0

You can use

 gender1 = Gender.valueOf(JOptionPane.showInputDialog(null, "Type Male or Female").toUpperCase());
nagendra547
  • 5,672
  • 3
  • 29
  • 43