不会CV的猿谋猿 2021-12-21 10:12 采纳率: 50%
浏览 13
已结题

怎么获取查找单词的下标啊

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class file {
int line=0;//记录行数
String str="";//存放文件读出的内容
String word;//存放查找的单词
int count;//记录单词出现的次数
public void newFile() throws IOException {
System.out.println("请输入文件地址:");
Scanner scanner=new Scanner(System.in);
String path =scanner.next();
File file = new File(path);
if(!file.exists()){
file.getParentFile().mkdirs();
}
file.createNewFile();

    // write
    boolean flag=false;
    System.out.println("是否输入文本(true/false)");
    flag=scanner.nextBoolean();
    if(flag==true) {

        FileWriter fw = new FileWriter(file, true);
        BufferedWriter bw = new BufferedWriter(fw);
        System.out.println("请输入文件内容:");
        System.out.println("提示:每个单词间需空一格");
        Scanner s = new Scanner(System.in);
        String content = s.nextLine();
        bw.write(content);
        bw.flush();
        bw.close();
        fw.close();
        System.out.println("文件创建成功");
    }

}

public void Lookup() throws IOException{
    System.out.println("请输入文件地址:");
    Scanner scanner=new Scanner(System.in);
    String path =scanner.next();
    File file = new File(path);

    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    System.out.println("请输入要查询的单词:");
    word=scanner.next();
    char[]w=word.toCharArray();
    while((str=br.readLine())!=null) {
        line++;
        char[]st=str.toCharArray();
        if(KMP(st,w)==true) {   //查询单词位置,true表示该单词出现在这一行
            System.out.println("在第" + line + "行");
        }
    }
    br.close();
    fr.close();

}

展开全部

  • 写回答

0条回答 默认 最新

      编辑
      预览

      报告相同问题?

      问题事件

      • 系统已结题 12月28日
      • 创建了问题 12月21日
      手机看
      程序员都在用的中文IT技术交流社区

      程序员都在用的中文IT技术交流社区

      专业的中文 IT 技术社区,与千万技术人共成长

      专业的中文 IT 技术社区,与千万技术人共成长

      关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

      关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

      客服 返回
      顶部