小编lan*_*cer的帖子

在文件中搜索String,如果找到则返回该String

如何在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"文件包含名称列表.

java io java.util.scanner

7
推荐指数
1
解决办法
7万
查看次数

标签 统计

io ×1

java ×1

java.util.scanner ×1