Create a new enumeration class:
Public enum enum1 {red ("Scarlet"), BLACK, GREEN, YELLOW, BLUE; Private String cum; Private enum1 () {//Private construction method (must be set to private because the enumeration itself is a singleton mode.) ) } private enum1 (String cum) { this. Cum= cum; } public String getcum () { return cum; }}
The values () method gets all the enumeration constants, and you can use the Foreach Traversal for example:
Determine which enumeration constants belong to the input values
String input = in.nextline (); for (Enum1 name:enum1.values ()) { if(Input.equals (Name.getcum ())) { System.out.println ( name); } Else { System.out.println ("null"); } }
The above method, if the input is red, gets the result:
Input: Red NULL NULL NULL NULL
Enum1.valueof (String str), which is enumerated constants such as:
The result of enum1.valueof ("Red") is: Red
Enum class enum case