1. 程式人生 > >Java正則表達式草稿程序*2

Java正則表達式草稿程序*2

cep temp mys a-z reader 程序 統計 lse ash

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