I have a class of fields that is like the following:
public class EmployeeInformationNames { private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; }
If I have a string
String str = "First Name: George\nLast Name: Brown";
I want to parse the string and store it in a map such that
Map<String, String> myMap = new HashMap<String, String>();
Where my key would be George and my value would be Brown.
I am not to sure how I would go about doing this.
I would suggest the following:
String[] parsedInput = str.split("\n"); String firstName = parsedInput[0].split(": ")[1]; String lastName = parsedInput[1].split(": ")[1]; myMap.put(firstName,lastName);
Source: Stackoverflow.com