如何在txt文件中搜索用户输入的String,然后将该String返回到控制台.我写了一些下面不起作用的代码,但我希望它可以说明我的观点......
public static void main(String[] args) {
searchforName();
}
private static void searchForName() throws FileNotFoundException {
File file = new File("leaders.txt");
Scanner kb = new Scanner(System.in);
Scanner input = new Scanner(file);
System.out.println("Please enter the name you would like to search for: ");
String name = kb.nextLine();
while(input.hasNextLine()) {
System.out.println(input.next(name));
}
}
Run Code Online (Sandbox Code Playgroud)
"leaders.txt"文件包含名称列表.