This could be the problem
// 1. Split `formatedString` as a List of Persons by `,`
ArrayList PersonList = new ArrayList(Arrays.asList(formatedString.split(",")));
// 2. Walk through the list of persons
for (int i = 0; i < PersonList.size(); i++) {
// 3. Split the same `formatedString` in fields ?!?
// What should happen here?
// II think this is your problem
String []value=formatedString.split(",");
insertPerson(value[0], value[1], value[2], value[3], value[4]);
System.out.println(PersonList.toString());
}
UPDATE
A working example with your data (linebreaks between the persons - spaces between the fields)
public class Splitter {
public static void main(String[] args) {
String values = "1 Ola Hansen Timoteivn Sandnes\n"
+ "2 Tove Svendson Borgvn Stavanger\n"
+ "3 Kari Pettersen Storgt Stavanger";
String[] rows = values.split("\n");
for (String row : rows) {
String[] fields = row.split(" ");
System.out.println(String.format("id: %s firstname: %s lastname: %s street: %s city %s", fields[0], fields[1], fields[2], fields[3], fields[4]));
}
}
}
UPDATE 2
This example reads the data from a file (/tmp/test.txt
)
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class Splitter {
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream("/tmp/test.txt")));
String row;
while ((row = input.readLine()) != null) {
String[] fields = row.split(" ");
System.out.println(String.format("id: %s firstname: %s lastname: %s street: %s city %s", fields[0], fields[1], fields[2], fields[3], fields[4]));
}
}
}
The file looks like this
1 Ola Hansen Timoteivn Sandnes
2 Tove Svendson Borgvn Stavanger
3 Kari Pettersen Storgt Stavanger