-3

我想从文本文件中搜索字符存在的时间。例如,一个文本文件包含

a bbb a abab bnbn

我只想读a而不是a。我希望结果是:

a = 2

private void compute3() {
        String[] reservedWord = new String[] {"a", "b", "c", "d", "e", "f", "g", "h",
        "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C",
        "D", "E", "F", "G", "X", "Y", "Z"};
        ArrayList<String> cntStr = new ArrayList<String>();
         int cnt=0;
         int cnt3=0;
            try {
        FileInputStream fis = new FileInputStream(chooser.getSelectedFile());
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));

        while(br.ready()){

                    StringTokenizer st = new StringTokenizer(br.readLine(), " ");

                    while(st.hasMoreTokens()){
                        String txt = st.nextToken();
                        for(cnt = 0;cnt<reservedWord.length;cnt++){
                            if(txt.contains(reservedWord[cnt])){
                                cntStr.add(reservedWord[cnt]);

                            }
                        }
                    }

                }

        br.close();

                int cnt2;
                ArrayList<String> counterPerWord = new ArrayList<String>();
                int counter = 0;

                for(cnt = 0;cnt<reservedWord.length;cnt++){

                    counter = 0;

                    for(cnt2 = 0;cnt2<cntStr.size();cnt2++){

                        if(reservedWord[cnt].equals(cntStr.get(cnt2))){

                        counter++;
                        }

                    }
                    //counter which is int becomes String
                    counterPerWord.add(counter + "");
                }

                cnt = 0;
                int N2 = 0;
                int n2 = 0;
                while(cnt!=counterPerWord.size()){
                    jTextArea6.append(reservedWord[cnt] + " = " + counterPerWord.get(cnt) + "\n");
                    System.out.println(reservedWord[cnt] + " = " + counterPerWord.get(cnt));
                    N2 = N2 + Integer.parseInt(counterPerWord.get(cnt));
                    cnt++;

                }

                   if(n2 >= 1){
                       n2++;
                   }


                //N1 = N1 * 2;
                jTextField7.setText("" + N2 + "");
                System.out.println("N2: " + N2);
                System.out.println("");
                jTextField5.setText("" + n2 + "");
               //System.out.println("n1:" + n1);

            } catch (Exception e) {
            e.printStackTrace();

    }}

它还从单词中获取其他字符。

4

1 回答 1

0
public static void main(String[] args) throws Exception {
    FileReader fr = new FileReader("test.txt");// your file path
    BufferedReader br = new BufferedReader(fr);
    String line = "";
    String singleChar = ""; // to store single length character

    while ((line = br.readLine()) != null) {
        String[] tokens = line.split(" ");
        for (int i = 0; i < tokens.length; i++) {
            if (tokens[i].trim().length() == 1) {
                singleChar = singleChar + tokens[i]; // storing single
                // length character
            }
        }

    }
    char[] charArray = singleChar.toCharArray();// converting singleChar to
    // char Array.

    Map<Character, Integer> map = new HashMap<Character, Integer>(); // Map
    // to store character and its frequency.
    for (int i = 0; i < charArray.length; i++) {
        if (Character.isAlphabetic(charArray[i])) { // checking the given
            // character is alphabet . Its means it will not count digits or
            // anything other that alphabet.
            Integer count = map.get(charArray[i]); // if not in map
            if (count == null)
                map.put(charArray[i], 1);
            else
                map.put(charArray[i], count + 1);
        }
    }

    for (Map.Entry<Character, Integer> entry : map.entrySet())
        // Iterating over map and printing character and its frequency
        System.out.println("Character is " + entry.getKey()
                + " and its count is" + entry.getValue());

}

Text in file(Input):

a bbb a abab bnbn a a b b A A B B 1 1 2 2 

Output:

Character is b and its count is2
Character is A and its count is2
Character is B and its count is2
Character is a and its count is4

Try this . I think this is what you exactly want.

于 2013-02-01T06:48:40.127 回答