Reputation: 59
Im writing code that needs to take a file input and then reconstruct the values into an ArrayList.
This is the code ive done so far to convert a file to an object.
public static ArrayList<User> fileToObject() {
ArrayList<User> FileToObject = new ArrayList<>();
Scanner in = null;
boolean err0 = false;
try {
in = new Scanner(Paths.get("User_info.txt"));
} catch (IOException ex) {
err0 = true;
}
if (!err0) // if couldn't open file then skip
{
while (in.hasNext()) {
String theUser = in.nextLine();
String[] Line = theUser.split(",");
User user = null;
try {
if ( Line.length == 10) {
// reconstruct Address
Address a1 = new Address( Line[2], Line[3], Line[4], Line[5], Line[6],
Integer.parseInt( Line[7]));
// reconstruct Customer
user = new Customer( Line[0], Line[1], Line[2], Line[3], Line[4], Line[5], Line[6], Line[7],a1, Line[14]);
FileToObject.add(user);
} else {
return null;
}
} catch (NoSuchElementException ex) {
System.out.println("File improperly formed. Terminating.");
}
}
in.close();
}
// write object back to file.
File f1 = new File(Paths.get("User_info.txt").toUri());
Formatter out = null;
boolean err = false;
try {
out = new Formatter(f1);
} catch (FileNotFoundException ex) {
err = true;
}
if (!err) {
//System.out.println(e3.size());
for (User i : FileToObject) {
out.format("%s%n", i.toString()); // output the card object to cvs format
}
out.close();
}
}
}
My issue here is that element Line[5] of the array User( user = new Customer(Line[0], Line[1], Line[2], Line[3], Line[4], Line[5)) is of Type enum(as seen in the first constructor of the below customer class) so i get an error "Incompatible Type, String cannot be converted to UserType(UserType being the Enum).
import java.util.ArrayList;
public class Customer extends Billing {
Address customerAddress;
public Customer(String id, String firstName, String lastName, String userName, String password, UserType userType, PermissionType permission, Boolean Status, Address billingAddress, String email, Address customerAddress) {
super(id, firstName, lastName, userName, password, userType, permission, Status, billingAddress, email);
this.customerAddress = customerAddress;
permission = PermissionType.Booking;
userType = UserType.VIP;
setId(id);
setPermission(permission);
setCustomerAddress(billingAddress);
}
public Customer(String id, String firstName, String lastName, String userName, String password, UserType userType, PermissionType permission, Boolean Status, Address billingAddress, String email) {
super(id, firstName, lastName, userName, password, userType, permission, Status, billingAddress, email);
}
public Address getCustomerAddress() {
return customerAddress;
}
public void setCustomerAddress(Address customerAddress) {
this.customerAddress = customerAddress;
}
I spoke to my Tutor and he told me i need to convert the String to an Enum. Im unsure of what he meant by this as he wouldnt explain any Further. Is he talking about the String Array or do i need to convert the String within the file im reading to an enum? Im a bit lost so any help is greatly appreciated
Upvotes: 0
Views: 500
Reputation: 434
your solution is so long. there is an elegant way of doing this
Properties properties = new Properties();
properties.load(new FileInputStream(new File("FileLocation")));
properties.values();
File can contain, list of values, key value pairs. At line no 2 file is loaded in Properties object. Now u can process it according to your need.
Upvotes: 0
Reputation: 484
If UserType.valueOf
could not provide what you need, add a static method that returns specific UserType
instance. For example:-
public enum UserType{
CUSTOMER("customer");
private String name;
UserType(String name){
this.name = name;
}
//A Map that holds user type name as key.
private static Map<String,UserType> userTypeMap = new HashMap<>();
//Populate userTypeMap with UserType instance
static{
for(UserType type : values()){
userTypeMap.put(type.name, type);
}
}
public static UserType of(String name){
return userTypeMap.get(name);
}
}
Callers could get UserType
instance as below:-
UserType type = UserType.of("customer");
Upvotes: 0
Reputation: 2981
If UserType
is an enum
, you can convert it with the static UserType.valueOf()
method, i.e.:
UserType userType = UserType.valueOf(Line[5]);
Now you can use userType
in place of Line[5]
in the following code.
Note that Line[5]
must exactly match the spelling of any of the enum types, or an IllegalArgumentException
will be thrown.
Upvotes: 2