java学习第三天

来源:互联网 发布:避雷针高度计算软件 编辑:程序博客网 时间:2024/04/29 03:12

------练习3



              读取文件中的一段英文,并可以显示任意一种单词的出现行数。

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