码迷,mamicode.com
首页 > 编程语言 > 详细

Java正则表达式草稿程序*2

时间:2018-12-29 23:11:03      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:man   dma   nbsp   数学   sys   pac   正则表达   turn   trace   

1、成绩统计。

输入文件input.txt:

张三 语文12 数学31 英语11
李四 语文22 数学22 英语22
王五 语文33 数学33 英语33

期待输出output.txt:

张三 语文12 数学31 英语11 总分54
李四 语文22 数学22 英语22 总分66
王五 语文33 数学33 英语33 总分99

代码:

package com.my.test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AnalysisResult {
    private static Pattern r = Pattern.compile("(\\d+)");
    private static int getTotal(String line) {
        Matcher m = r.matcher(line);
        int total = 0;
        while (m.find()) {
            total += Integer.valueOf(m.group(0));
        }
        return total;
    }

    public static void main(String args[]) {
        try {
            /* 读入TXT文件 */
            File readFile = new File("input.txt");
            InputStreamReader inputStreamReader = new InputStreamReader(
                    new FileInputStream(readFile));
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            List<String> contents = new ArrayList<>();
            while ((line = bufferedReader.readLine()) != null) {
                contents.add(line);
            }
            bufferedReader.close();

            /* 写入TXT文件 */
            File writeFile = new File("output.txt");
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(writeFile, true));
            PrintWriter pw = new PrintWriter(bufferedWriter);
            for (int i = 0; i != contents.size(); ++i) {
                String temp = contents.get(i);
                pw.println(temp + " 总分" + getTotal(temp));
            }
            bufferedWriter.flush();
            bufferedWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

/

2、单词统计。

输入文件input.txt:

Today, when I go home after school, I find that I lose my key.
I feel so worried and try to remember all the places I have been to.
As I walk into the shop, an old man stops me and asks if I lose the key.
I feel so excited and say yes. At last, I find my key and go home.
Though I am lucky this time, I tell myself not to do it again.

期待输出:

技术分享图片

代码:

package com.my.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordStatistic {

    private static int total = 0;
    private static Pattern r = Pattern.compile("(\\b[a-zA-Z]+\\b)");
    private static Map<String, Integer> wordMap = new HashMap<>();
    private static void countWords(String line) {
        Matcher m = r.matcher(line);
        while (m.find()) {
            total++;
            String word = m.group(0);
            Integer wordCount = wordMap.get(word);
            if (wordCount != null) {
                wordMap.put(word, wordCount + 1);
            } else {
                wordMap.put(word, 1);
            }
        }
    }

    private static List sortByValue(Map<String, Integer> map) {
        List<Map.Entry<String, Integer>> result = new ArrayList<>(map.entrySet());
        result.sort(Comparator.comparing(Map.Entry::getValue));
        return result;
    }

    public static void main(String[] args) {
        try {
            /* 读入TXT文件 */
            File readFile = new File("input.txt");
            InputStreamReader inputStreamReader = new InputStreamReader(
                    new FileInputStream(readFile));
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                countWords(line);
            }
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("单词总数:" + total);
        System.out.println("不同单词的个数:" + wordMap.size());
        List list = sortByValue(wordMap);
        for (int i = list.size() - 1; i != 0; --i) {
            System.out.println(list.get(i));
        }
    }
}

 

Java正则表达式草稿程序*2

标签:man   dma   nbsp   数学   sys   pac   正则表达   turn   trace   

原文地址:https://www.cnblogs.com/xkxf/p/10197837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!