java - 计算单词中的字母出现次数

标签 java string char

<分区>

我试图让我的 java 程序计算每个单词中的字母数。现在我用它来计算单词,而不是字母。如果能帮助它写信,那就太好了!

import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.TreeSet;
import java.util.Scanner;

public class LetterTypeCount 
{
    public static void main(String[] args) {
        // create HashMap to store String keys and Integer values
        Map<String, Integer> myMap = new HashMap<>();

        createMap(myMap); // create map based on user input
        displayMap(myMap); // display map content
    }

    // create map from user input
    private static void createMap(Map<String, Integer> map)
    {
        Scanner scanner = new Scanner(System.in); // create scanner
        System.out.println("Enter a string:"); // prompt for user input
        String input = scanner.nextLine();

        // tokenize the input
        String[] tokens = input.split(" ");

        // processing input text
        for (String token : tokens)
        {
            String letter = token.toLowerCase(); // get lowercase letter

            // if the map contains the letter
            if (map.containsKey(letter)) // is letter in map
            {
                int count = map.get(letter); // get current count
                map.put(letter, count + 1); // increment count
            }
            else
                map.put(letter, 1); // add new letter with a count of 1 to map
        }
    }

    // display map content
    private static void displayMap(Map<String, Integer> map)
    {
        Set<String> keys = map.keySet(); // get keys

        // sort keys
        TreeSet<String> sortedKeys = new TreeSet<>(keys);

        System.out.printf("%nMap contains:%nKey\t\tValue%n");

        // generate output for each key in map
        for (String key : sortedKeys)
            System.out.printf("%-10s%10s%n", key, map.get(key));
        System.out.printf("%nsize: %d%nisEmpty: %b%n",
                map.size(), map.isEmpty());
    }
} // end class LetterTypeCount

我想我需要在某处使用 String charAt 方法

最佳答案

可能不是很优雅但是:

public class LetterTypeCount 
{
    public static void main(String[] args) {
        // create HashMap to store String keys and Integer values
        Map<String, Integer> myMap = new HashMap<>();

        createMap(myMap); // create map based on user input
        displayMap(myMap); // display map content
    }

    // create map from user input
    private static void createMap(Map<String, Integer> map)
    {
        Scanner scanner = new Scanner(System.in); // create scanner
        System.out.println("Enter a string:"); // prompt for user input
        String input = scanner.nextLine();

        // split to words
        String[] words = input.split(" ");
        for (String word : words) 
        {
            word = word.toLowerCase(); // get lowercase word
            for(int i=0; i<word.length(); i++) 
            {
                char c = word.charAt(i); //get char at position i
                if (map.containsKey(c + "")) // is letter in map
                {
                    int count = map.get(c + ""); // get current count
                    map.put(c + "", count + 1); // increment count
                }
                else
                    map.put(c + "", 1); // add new letter with a count of 1 to map
            }
            // if the map contains the letter
        }
    }

    // display map content
    private static void displayMap(Map<String, Integer> map)
    {
        Set<String> keys = map.keySet(); // get keys

        // sort keys
        TreeSet<String> sortedKeys = new TreeSet<>(keys);

        System.out.printf("%nMap contains:%nKey\t\tValue%n");

        // generate output for each key in map
        for (String key : sortedKeys)
            System.out.printf("%-10s%10s%n", key, map.get(key));
        System.out.printf("%nsize: %d%nisEmpty: %b%n",
                map.size(), map.isEmpty());
    }
} // end class LetterTypeCount

关于java - 计算单词中的字母出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30103426/

相关文章:

JavaFX TreeView 在任何深度匹配给定值找到 TreeItem

java - 如何将变量从 java 类加载到不同的类

c# - someString.IndexOf(someString) 在 .NET 4 下返回 1 而不是 0

c++ - C/C++ strcpy 未处理的读取冲突

c - C中的函数打印字符内容

java - PropertyPlaceholderConfigurer 与 Hibernate.cfg.xml

string - 拆分文本并按空格获取字符串数组,如果文本长度超过 500,则获取字符串数组

string - 如何循环遍历目录并跳过包含特定字符串的文件夹?

java - 为什么 System.out.println(+c1) 和 System.out.println ("c1"= +c1) 之间存在差异?

java - 在 JDBC/iBatis 3 中迭代大型结果集的最佳方法是什么?