有点困惑
我有一个这样格式化的文本文件
足球,男,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错误
有任何想法吗
谢谢
最佳答案
首先,使用定界符","
分割行:
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));
关于java - 从文本文件读取时如何分割,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46097340/