You can split by the newline character first.
String lines = na.split("\n");
When looping through lines
, split each line by whitespace, \\s+
, to split each field on the current line.
for (String line : lines)
{
String[] text = line.split("\\s+");
// other processing
}
Then you can access the individual values and assign them to arrays or whatever you'd like. Your idea of storing 2 arrays would work fine, but I would create an array or a List
of Person
objects that are defined to hold your fields such as name and age.