0

I want to write a code that will show the user to input either a male or a female when asked to enter the sex. Firstly I created a class called SexeEnum as shown below

public enum SexeEnum {
    Male,
    Female
}

Then I want to ask the user to choose between the male or the female gender but I don't know how to proceed as shown below:

public static void main(String[] args) {             
    Utilisateur utilisateur = new Utilisateur();
    Scanner u = new Scanner(System.in);
    System.out.println("Enter your Name: ");
    utilisateur.setNom(u.nextLine());
    System.out.println("Enter your Sexe: ");
    System.out.println("1-Male ");
    System.out.println("2-Female ");
    utilisateur.setSexe(u.nextLine());
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 6
    Please stop tagging your Java questions `javascript`. They are unrelated languages. – khelwood May 27 '21 at 21:36
  • https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_is_JavaScript – Mister Jojo May 27 '21 at 21:39
  • 1
    You can assign numeric values to `enum` members. – PM 77-1 May 27 '21 at 21:41
  • Does this answer your question? [Is it possible to assign numeric value to an enum in Java?](https://stackoverflow.com/questions/8811815/is-it-possible-to-assign-numeric-value-to-an-enum-in-java) – PM 77-1 May 27 '21 at 21:42
  • Another approach is letting the user write "Male" or "Female": [Scanner with Enum, Java](//stackoverflow.com/q/36035763) – Tom May 27 '21 at 21:46

1 Answers1

1

Try:

import java.util.Scanner; 

class Main {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);  
    System.out.println("Enter Sex");

    String userName = myObj.nextLine();  
    System.out.println("Sex is: " + userName);  
  }
}
BLUBBER
  • 93
  • 1
  • 10