查询某个字符串在文章中出现的次数,列出行数

来源:互联网 发布:避雷针高度计算软件 编辑:程序博客网 时间:2024/04/29 10:14
package com.company;


import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Main
{


    public static void main(String[] args) throws IOException
    {
        File f = new File("D:/1.txt");
        FileReader fr = new FileReader(f);
        BufferedReader br = new BufferedReader(fr);
        List<String> list = new ArrayList<String>();
        int Linenum = 1;
        String s = null;
        while ((s = br.readLine()) != null)
        {
            list.add(s);
        }
        System.out.println("输入要查找的单词");
        Scanner sc = new Scanner(System.in);
        String strWord = sc.nextLine();
        for (String b : list)
        {
            boolean a = ContainsStr(b,strWord);
            if(a){
                System.out.println(Linenum+" "+b);
            }Linenum++;
        }


    }


    public static boolean ContainsStr(String s1, String s2)
    {
        if (s1.indexOf(s2) >= 0)
        {
            return true;
        } else
        {
            return false;
        }


    }
}
0 0
原创粉丝点击