Hi,
In the application there is a string in the following format:
String elements = "[11, john,][23, Adam,][88, Angie,]..." (... means there are more elements in the string)
From the given string I have to create an ArrayList for name IDs (11, 23, 88, ...) and ArrayList for names (john, Adam, Angie, ...)
I created two methods:
private int getItemID(int listLocation, String inputString){
int indexBeginning = inputString.indexOf("[", listLocation) + 1;
int indexEnd = inputString.indexOf(",", listLocation) - 1;
String sID = inputString.substring(indexBeginning, indexEnd);
int result = Integer.parseInt(sID);
return result;
}
private String getItemName(int listLocation, String inputString){
int indexBeginning = inputString.indexOf(" ", listLocation) + 1;
int indexEnd = inputString.indexOf(",", indexBeginning) - 1;
String result = inputString.substring(indexBeginning, indexEnd);
return result;
}
and intend to use these two methods in the method parseArrayString(String inputString), which I haven't written yet but would work the following way:
private void parseCommunityList(String inputString){
int currentLocation = 0;
int itemsCount = count the number of "[" characters in the string
for(int i = 0; i < itemsCount; i++)
{
currentLocation = get the location of the (i)th character "[" in the string;
String name = getItemName(currentLocation, inputString);
int ID = getItemID(currentLocation, inputString);
nameArray.Add(name);
idArray,Add(ID);
}
}
I would appreciate if anyone of you could suggest any simpler way to create two ArrayLists from the given string.
Thanks!