gpt4 book ai didi

java - JAVA中提取特定字符串

转载 作者:行者123 更新时间:2023-12-01 18:41:41 27 4
gpt4 key购买 nike

我的文件中的数据如下:

name: "Clooney, George", release: "2013", movie: "Gravity", imdb: "9",
name: "Pitt, Brad", release: "2004", movie: "Ocean's 12", imdb: "8",
name: "Clooney, George", release: "2004", movie: "Ocean's 12", imdb: "8",
name: "Pitt, Brad", release: "1999", movie: "Fight Club", imdb: "9",

我需要输出如下:

name: "Clooney, George", movie: "Gravity",
name: "Clooney, George", movie: "Ocean's 12",
name: "Pitt, Brad", movie: "Ocean's 12",
name: "Pitt, Brad", movie: "Fight Club",

我对 Java 还很陌生。有人可以给我一些关于如何完成这项工作的提示和指示吗?

谢谢,TM

最佳答案

public static void main (String [] args){
File file = new File("data.txt");

Scanner scanner;
try {
scanner = new Scanner(file);

while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);

line = line.substring(7, line.length()-1);
String name = line.substring(0, line.indexOf("\"")-1);

int index = line.indexOf("movie: \"");
line = line.substring(index+8, line.length()-1);
String movie = line.substring(0, line.indexOf("\"")-1);

System.out.println("name: \"" + name + "\", movie: \"" + movie + "\",");
}
scanner.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

关于java - JAVA中提取特定字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19741378/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com