Java文件I / O:我应该在什么条件下停止在文本文件中重复写入字母频率?

时间:2016-04-22 15:06:50

标签: java file-io character

所以我接近完成java中字符频率的代码。因此,方向是获取文本文件并将字符频率(从A-Z,a-z和0-9)保存到另一个文本文件中,但频率的数量在那里排序。这就是我想出的:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.Scanner;

    import javax.swing.JFileChooser;

public class Machine_Exer6 {

public void charfrequency() {
    // TODO Auto-generated method stub
    File infile = null;
    File result = null;
    Scanner myscan = null;
    JFileChooser fc= new JFileChooser();

    fc.showOpenDialog(null);
    infile = fc.getSelectedFile();

    fc.setSelectedFile(new File(""));

    fc.showSaveDialog(null);
    result = fc.getSelectedFile();

    try {
        myscan = new Scanner(infile);
        while(myscan.hasNextLine()){
            String str = myscan.nextLine();
            Alphanumeric(str, result);
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        myscan.close();
    }



}

private void Alphanumeric(String str, File result) {
    // TODO Auto-generated method stub


    int ctr=0;
    PrintWriter printtofile = null;
    try {
        printtofile = new PrintWriter(result);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try{
        for(int j=0; j<str.length(); j++){
            for (int i=0; i<str.length(); i++){
                if((str.charAt(j)==str.charAt(i) || str.charAt(j)==Character.toUpperCase(str.charAt(i))) && (Character.isAlphabetic(str.charAt(j)) || Character.isDigit(str.charAt(j)))){
                    ctr++;
                }

            }

            if (ctr!=0){
                printtofile.println(str.charAt(j)+": "+ctr);
                ctr=0;
            }

        }
    }finally{
        printtofile.close();
    }

}

但是当我尝试阅读文本文件时,比如说,其内容为:

  

“每当

     

无论何处何处

     

你是

     

我会爱,爱,爱。“

创建的文本文件如下:

  

我:2

     

w:1

     

我:2

     

l:5

     

l:5

     

l:5

     

o:3

     

v:3

     

e:3

     

l:5

     

o:3

     

v:3

     

e:3

     

l:5

     

o:3

     

v:3

     

e:3

此外,小写字母不包含在小写字母中。有什么方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助:

import java.io.*;
import java.util.*;

public class Frequency {

    private static final int DIGIT_START = 48;
    private static final int DIGIT_END   = 57;
    private static final int UPPER_START = 65;
    private static final int UPPER_END   = 90;
    private static final int LOWER_START = 97;
    private static final int LOWER_END   = 122;

    private final BufferedReader reader;
    private final Map<Character, Integer> frequencyCounter;

    public Frequency(File file) throws FileNotFoundException {

        reader = new BufferedReader(new FileReader(file));
        frequencyCounter = new HashMap<>();
    }

    public void calculateFrequency() throws IOException {

        int input;

        while((input = reader.read()) != -1){
            if(isAlphaNeumeric(input)){
                char validInput = (char)Character.toLowerCase(input);
                if(!frequencyCounter.containsKey(validInput)){
                    frequencyCounter.put(validInput, 1);
                }
                else{
                    frequencyCounter.put(validInput,
                       frequencyCounter.get(validInput) + 1);
                }
            }
        }
    }

    public boolean isAlphaNeumeric(int toTest){

        return isAlphaNewNeumericHelp(toTest, DIGIT_START, DIGIT_END) ||
                isAlphaNewNeumericHelp(toTest, UPPER_START, UPPER_END) ||
                isAlphaNewNeumericHelp(toTest, LOWER_START, LOWER_END);
    }

    private boolean isAlphaNewNeumericHelp(int toTest, 
                                           int lowerBound,          
                                           int upperBound){

        return toTest >= lowerBound && toTest <= upperBound;
    }

    public void printFrequency(){

        final List<Map.Entry<Character, Integer>> stringCounts = 
            new ArrayList<>(frequencyCounter.entrySet());

        Collections.sort(stringCounts, 
            (o1, o2) -> o1.getKey().compareTo(o2.getKey()));

        stringCounts.forEach(System.out::println);
    }

    public static void main(String[] args) throws IOException {

        Frequency frequency = new Frequency(new File("file"));
        frequency.calculateFrequency();
        frequency.printFrequency();
    }
}

Map跟踪所有计数。在确定某个字符有效后,将其强制为小写并将其强制转换为char。如果Map没有与有效字符对应的条目,则添加一个,否则更新Map值侧。现在只需将已排序的结果打印到控制台。

希望它有所帮助。