I would load from a file a string and put in in a object enum.
I create into a class this:
public Passenger{
private String pass_name;
public enum State{
b,c,d;
};
....
State reser;
public Passenger(String n,State r)
pass_name=n;
reser=r;
}
in another file I want read a file and put the string in the way i can create an object passenger such:
Passenger p=new Passenger(p_name,What should i put here)?
here is the structure of my file:
cod
passenger_name
reverved
cod and passenger will be rapresented by String while reserved should be rapresented through enum. I will read the file:
BufferedReader reader=new BufferedReader(new FileReader(fname));
String cod=reader.readLine();
while(cod!=null){
String p_name=reader.readLine();
how can i load a enum type?
Passenger p=new Passenger(p_name,What should i put here)?
cod=reader.readLine();
}
Use
YourEnumType.valueOfto convert the String you read from the file into the enum:(I took the liberty of making a cleaner version of your sample code.)
Make sure that the string in the file is the same as the name of your enum constant, including capitals, and that it doesn’t include extra whitespace.