[英]java tokenize strings read from a text file
I have this program that reads a text file from my computer. 我有这个程序从我的电脑读取文本文件。 the text file is a list of titles.
文本文件是标题列表。 Im trying to figure out how to tokenize what is returned so that I get the full name instead of a piece.
我试图弄清楚如何标记返回的内容,以便我得到全名而不是一块。 All the examples I am able to find involve regular arrays and Im using an arraylist.
我能找到的所有例子都涉及常规数组和Im使用arraylist。 I need to be able to read each string back into my arraylist and cut it off where the & are.
我需要能够将每个字符串读回我的arraylist并将其剪切掉。
The Text file looks like this: Text文件如下所示:
Star Wars& DVD& 星球大战&DVD&
Lord of the Rings& DVD& 指环王和DVD&
Resident Evil& DVD& 生化危机和DVD&
public static void main(String[] args) {
File f = new File("file.txt");
try {
ArrayList<String> lines = get_arraylist_from_file(f);
for (int x = 0; x < lines.size(); x++) {
System.out.println(lines.get(x));
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("done");
}
public static ArrayList<String> get_arraylist_from_file(File f)
throws FileNotFoundException {
Scanner s;
ArrayList<String> list = new ArrayList<String>();
s = new Scanner(f);
while (s.hasNext()) {
list.add(s.next());
}
s.close();
return list;
}
while (s.hasNextLine()) {
list.add(s.nextLine().replaceAll("&", ""));
}
removes the ampersands 删除&符号
public static ArrayList<String> get_arraylist_from_file(File f) throws FileNotFoundException {
Scanner scanner;
ArrayList<String> list = new ArrayList<String>();
scanner = new Scanner(f);
String s = scanner.nextLine();
while(scanner.hasNextLine()){
String[] tempList = s.split("&"); //This will give you this [title][ DVD]
String title = tempList[0];
String type = tempList[1].subString(1); //If you want the input at the place of DVD, with the space removed
list.add(title);
s = scanner.nextLine();
}
scanner.close();
return list;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.