首页 > 编程语言 >重复字母对(Java_文件IO操作)

重复字母对(Java_文件IO操作)

时间:2023-06-20 10:04:50浏览次数:47  
标签:maxx Java words temp 字母 IO new txt sum


题目

在words.txt文件中包含了87314个单词,编写Java程序从words文件中读取单词,并输出重复字母对最多的单词,将最多重复字母对的单词写入newwords.txt文件中。例如tooth这个单词有一个重复字母对,committee有三个重复字母对。

流程图

重复字母对(Java_文件IO操作)_txt文件

Code

package IO;

import java.io.*;

public class words {
    public static void main(String[] args){
        File file =new File("words.txt");
        File file2=new File("newwords.txt");
        word[] words=new word[100000];
        int maxx=0;
        char[] ch=new char[100000];
        try{
            FileReader in=new FileReader(file);
            FileWriter out=new FileWriter(file2, true);
            String str="", last="";
            int sum=0, i=0;
            while(in.read(ch, 0, 1)!=-1){
                String temp=new String(ch, 0, 1);
                if(temp.equals("\n")) {
                    words[i++]=new word(str, sum);
                    maxx=maxx>sum?maxx:sum;
                    str = "";
                    last="";
                    sum=0;
                }else if(temp.equals("\r"))
                    continue;
                else
                {
                    str+=temp;
                    if(last.equals(temp))
                        sum++;
                    last=temp;
                }
            }
            in.close();
            for(int j=0;j<i;j++) {
                if (words[j].len == maxx) {
                    out.write(words[j].val+"\n");
                }
            }
            out.close();
        }catch(IOException e){
            System.out.println(e);
        }
    }
}
class word{
    public word(){val="";len=0;}
    word(String a, int b){val=a;len=b;}
    String val;
    int len;
}

结果展示

重复字母对(Java_文件IO操作)_txt文件_02


标签:maxx,Java,words,temp,字母,IO,new,txt,sum
From: https://blog.51cto.com/u_16165815/6520494

相关文章

  • PAT_Advanced Level_1080 Graduate Admission(C++_模拟_快排_卡常)
    Itissaidthatin2011,thereareabout100graduateschoolsreadytoproceedover40,000applicationsinZhejiangProvince.Itwouldhelpalotifyoucouldwriteaprogramtoautomatetheadmissionprocedure.Eachapplicantwillhavetoprovidetwograd......
  • 简单计算器(Java_图形用户界面设计)
    题目编写一个应用程序,包括三个文本框和四个按钮,分别是“加”、“减”、“乘”、“除”,单击相应的按钮,将两个文本框的数字做运算,在第三个文本框中显示结果。布局Codepackageunit_9;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt......
  • java:保留两位小数输出,两种方法
    第一种方法://四舍五入保留publicstaticvoidmain(String[]args){Doubledata=1.23635345;DecimalFormatdf=newDecimalFormat("0.00");System.out.println(df.format(data));}第二种方法://四舍五入保留publicstaticvoidmain(String[]args){......
  • Pyinstaller打包 Pytest+Allure成exe文件执行时,报错ERROR: usage: apitest.exe [opti
    网上找了很多案例啊 都没解决问题,由本人的多次试验 终于成功解决1、打包运行 pyinstaller-D xxx.py  打包成功后 执行exe报错 如下 2、此情况是说明 命令无法正确识别 也就是说 未导入allure 相关三方库解决方案:修改xxx.spec 文件 添加对应三方库依赖......
  • 【Java】Map集合的遍历
    HashMap<String,String>map=newHashMap<>();map.put("001","赵");map.put("002","钱");map.put("003","孙");map.put("004","李......
  • Hessian Free Optimization——外国网友分享的“共轭梯度”的推导
    外国网友分享的“共轭梯度”的推导:https://andrew.gibiansky.com/blog/machine-learning/hessian-free-optimization/  =====================================  系数矩阵为Hessian矩阵时的使用Pearlmuttertrick的共轭梯度解法   Ax=b的迭代解法——......
  • android studio 各个版本区别
    在使用AndroidStudio时,建议使用最新的动物命名版本,以获取最新的功能和修复的错误。官网描述动物命名是重要版本(间接理解为稳定版本) 2022年9月 Dolphin(海豚)2022年5月 Chipmunk(花栗鼠)AndroidStudioFlamingo | 2022.2.1 (火烈鸟)2022年1月 Bumblebee(大......
  • 树莓派、PS4、Switch、STM32、安卓、iOS
    系统架构树莓派:基于ARMCortex-A系列处理器(如Cortex-A53)的Linux操作系统。PS4:基于x86-64架构的FreeBSD操作系统。Switch:基于ARMv8-A架构的NvidiaCustom操作系统,也被称为“HorizonOS”。STM32:无操作系统或基于实时操作系统(RTOS)的固件(裸机)编程。安卓:基于Linux内核的软件堆栈......
  • JavaScript判断两个数组相等的四类方法
    在JavaScript中,数组本质上是一种特殊的对象,它的类型值会返回object。如果我们需要比较两个数组是否相等,不能像比较基本类型(String、Number、Boolean等)一样,使用===(或==)来判断,所以如果要比较数组是否相等,需要使用一些特殊方法。关于JS类型的判断,可见博文typeof详解。本文......
  • Axios封装
    vue-axiosAxios是一个基于promise的HTTP库,可以用在浏览器和node.js中一、流程:1.拿到项目和后端接口,首先要配置全局代理;2.接着全局封装axios和request.js;3.过滤axios请求方式,控制路径,参数的格式,http.js;4.正式封装api.js;5.页面调用;二、具体(1)前......