I have an array of strings identifying a pet name, type of animal, age and their owner's name (each attribute is separated by a comma):
String[] petInfo = ["Spot, dog, 2, Joey", "Kip, dog, 3, Jack", "Snuffles, cat, 1, Jane" , "Franklin, turtle, 4, Arthur",...]
I created a pet class with members string petName, string type, int age and string ownerName.
public class Pet
{
string petName;
string type;
int age;
string ownerName;
//getters
}
In my main I declared
List<Pet> pets = new ArrayList<Pet>();
I want to create an array of pet objects using an arraylist. I know I have to use split to get through the original array but that leaves me with another array of comma separated values for one pet. How do I write a loop that can go through the petInfo array and add those values to each pet object in my list?
Here is all you need:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String[] petInfo = {"Spot, dog, 2, Joey", "Kip, dog, 3, Jack", "Snuffles, cat, 1, Jane" , "Franklin, turtle, 4, Arthur"};
List<Pet> pets = new ArrayList<Pet>();
for (String singlePetInfo : petInfo) {
pets.add(getPetFromString(singlePetInfo));
}
pets.forEach(System.out::println);
}
private static Pet getPetFromString(String petDescription){
String[] split = petDescription.split(", ");
return new Pet(split[0], split[1], Integer.parseInt(split[2]), split[3]);
}
}
A very verbose example: This method takes the array and converts the pet parts to a pet. Returning the list of pets at the end.
public List<Pet> toPets(String[] petArray) {
List<Pet> pets = new ArrayList();
for (String petAsString: petArray) {
String[] petParts = petAsString.split(",");
String petName = petParts[0].trim();
String type = petParts[1].trim();
int age = Integer.parseInt(petParts[2].trim());
String ownerName = petParts[3].trim();
Pet pet = new Pet(petName, type, age, ownerName);
pets.add(pet);
}
return pets;
}
I wrote a solution. There are two key points. Splitting string can cause faulty solutions. That's why, I added a proper regex. Also you need to take care of integer conversion. Using integer class makes it easy for you.
String[] petInfo = {"Spot, dog, 2, Joey", "Kip, dog, 3, Jack", "Snuffles, cat, 1, Jane" , "Franklin, turtle, 4, Arthur"};
List<Pet> pets = new ArrayList<Pet>();
for (int i = 0; i < petInfo.length; i++) {
String[] pet = petInfo[i].split("\\s*,\\s*");
pets.add(new Pet(pet[0], pet[1], Integer.parseInt(pet[2]), pet[3]));
}
You can create a Constructor or helper method to create Pet
object from given string.
Following is code snippet for constructor. See here the complete working code :
Pet(String val)
{
String[] vals = val.trim().split(",");
petName = vals[0].trim();
type = vals[1].trim();
age = Integer.valueOf(vals[2].trim());
ownerName = vals[3].trim();
}
To use it, you can do something like following:
String[] petInfo = new String[]{"Spot, dog, 2, Joey",
"Kip, dog, 3, Jack", "Snuffles, cat, 1, Jane" , "Franklin, turtle, 4, Arthur"};
ArrayList<Pet> list = new ArrayList();
for(String p:petInfo)
{
list.add(new Pet(p));
}
Take your petInfo
array and go through it with a for
loop. Then you split your string and create a new Pet
object, then you can just add it to your List<Pet>
called pets
List<Pet> pets = new ArrayList<Pet>();
//Renamed it to petInfos, for better usage in the for loop
String[] petInfos = ["Spot, dog, 2, Joey", "Kip, dog, 3, Jack", "Snuffles, cat, 1, Jane" ,
"Franklin, turtle, 4, Arthur",...]
for(String petInfo : petInfos){
String[] petInfoSplitted = petInfo.split(", ");
pets.add(new Pet(petInfoSplitted[0], petInfoSplitted[1],
Integer.parseInt(petInfoSplitted[2]), petInfoSplitted[3]);
}
I hope my awnser was helpfull.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.