相当有点困惑
我有一个格式化为
的文本文件FooBoo,雄性,20,10 / 04/1988
我知道如何分割字符串,例如
public loadData() {
String filepath = "G:\\Documents\\MEMove\\XXClients\\data.txt";
BufferedReader bufReader = new BufferedReader(new FileReader(filepath));
String line = bufReader.readLine();
while (line != null) {
// String[] parts = line.split("/");
String[] parts = line.split(",");
String part1 = parts[0];
String part2 = parts[1];
int part3 = Integer.parseInt(parts[3]);
String [] sDOB = line.split("/");
int sDOB1 = Integer.parseInt(sDOB[4]);
People nPeople = new People(part1,part2,part3,sDOB1);
readPeopleList.add(nPeople);
line = bufReader.readLine();
} //end of while
bufReader.close();
for(People per: readPeopleList)
{
System.out.println("Reading.." + per.getFullName());
}
}// end of method
问题是如何拆分DOB /它不起作用我得到NumberFormatException错误
任何想法
谢谢
答案 0 :(得分:1)
首先,使用分隔符","
分割线:
String line = "FooBoo,male,20,10/04/1988";
String[] parts = line.split(",");
然后,使用分隔符"\"
分割最后一部分:
String dob = parts[parts.length - 1];
String[] sDob = dob.split("/");
for (String s : sDob) {
System.out.println(s);
}
修改:您可以将sDob
转换为ArrayList
,如下所示:
ArrayList<String> sDobList = new ArrayList<>(Arrays.asList(sDob));