假设我有一个这样的文件:
Name: someone1 surname
Age: someNumber1
Height: whoCares1
Weight: someWeight1
Name: someone2
Age: someNumber2
Height: whoCares2
Weight: someWeight2
Name: someone3
Age: someNumber3
Height: whoCares3
Weight: someWeight3
我想在ArrayList<String>.
我只想为此使用Scanner类。
我尝试使用如下代码从文件中获取“名称:”部分:
while(scanner.hasNextLine()) {
//read line by line
String line = scanner.nextLine();
//code to search for a word in the file which
if(line.indexOf("Name:")!=-1) {
// will search line by line for this word ignoring numbers in addition
// I find the word with this, what next?
// I want to get word next to Name: and not only from one but from all the instances of the word Name:
}
else
System.out.println("does not");
}
我想在“名称:”旁边得到一个词,不仅来自一个词,而且来自“名称:”这个词的所有实例。
我怎样才能做到这一点?
编辑:
另外,如何在文件中分隔字符串,例如,如果我想将第一人称的姓名存储在文件中,如someone1一个 ArrayList 和surname另一个 ArrayList 中。我如何将它们分开,并将它们存储在新的 ArrayLists 中。
www说
繁华开满天机
获取特定子字符串后面的字符串
如何在Windows批处理文件中按空格分割字符串?
javascript中如何在字符串的两头加上一些字符
如何用c语言求一串字符串中英文字母的个数?
相关分类