如何计算Java文本文件中每个字母的出现次数?

我对 Java 很陌生,我正在为我的第一个任务而苦苦挣扎。任务是扫描一个文本文件(para1.txt)并通读它并计算每个字母出现的次数。(所以,它应该输出 a-57、b-21、c-12 等)出现。目前,我的代码为所有字母打印“17”,因为 para1.txt 文件中有 17 行。到目前为止,这是我的代码:


import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;


public class LetterCounter {

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


        Scanner input = new Scanner(new File("src/para1.txt"));


        int[] count = new int[26];


        while (input.hasNextLine()) {

            String answer = input.nextLine();

            answer = answer.toLowerCase();

            char[] characters = answer.toCharArray();


            for (int i = 0; i < 26; i++) {

                count[i]++;

            }

        }


        for (int i = 0; i < 26; i++) {

            StdOut.print((char) (i + 'a'));

            StdOut.println(": " + count[i]);

        }

    }

}


隔江千里
浏览 157回答 3
3回答

慕盖茨4494581

我认为您想获取实际的字母,并可能检查您是否真的有字母(而不是空白或数字)。public class LetterCounter {&nbsp; &nbsp; public static void main(String[] args) throws FileNotFoundException {&nbsp; &nbsp; &nbsp; &nbsp; Scanner input = new Scanner(new File("src/para1.txt"));&nbsp; &nbsp; &nbsp; &nbsp;int[] count = new int[26];&nbsp; &nbsp; &nbsp; &nbsp; while (input.hasNextLine()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String answer = input.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answer = answer.toLowerCase();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char[] characters = answer.toCharArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /// change here!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i< characters.length ; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((characters[i] >='a') && (characters[i]<='z')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count[characters[i] -'a' ]++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /// change ends.&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 26; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StdOut.print((char) (i + 'a'));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StdOut.println(": " + count[i]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

手掌心

创建一个以字符为键,以count(Integer)为值的hashmap。Hashmap存储键值对,其中key是唯一的,put()方法用于将特定的键和值插入hashmap。public class CharCount {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; File f1 = new File("file-path");&nbsp; &nbsp; &nbsp; &nbsp; HashMap<Character, Integer> charMap = new HashMap<Character, Integer>();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scanner in = new Scanner(f1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(in.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String inputString = in.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inputString = inputString.replace(" ", "");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char[] strArray = inputString.toCharArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (char c : strArray) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (charMap.containsKey(c)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If character is present in charMap, incrementing it's count by 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charMap.put(c, charMap.get(c) + 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If char is not present in charMap ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // putting this character to charMap with 1 as it's value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charMap.put(c, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Printing the charMap&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(charMap);&nbsp; &nbsp; &nbsp; &nbsp; } catch (FileNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

慕森卡

在您的循环中,您只需递增count数组的每个索引:for (int i = 0; i < 26; i++) {&nbsp; &nbsp; &nbsp;count[i]++;}相反,您可以做的是遍历characters数组,并在 char - 处增加索引'a',以获得正确的索引:for(char c : characters) {&nbsp; &nbsp;count[c - 'a']++;}唯一的问题是,如果有任何非字母字符,这将引发索引越界错误。您可能希望确保它在范围内:int index = c - 'a';if(index > 25 || index < 0) {&nbsp; &nbsp; System.out.println("Invalid character");} else {&nbsp; &nbsp;//process like normal}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java