我试图让我的程序在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方法
答案 0 :(得分:3)
可能不是很优雅但是:
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
答案 1 :(得分:1)
假设你只计算拉丁字符:
public int[] letters = new int['z' - 'a' + 1];
// count chars:
for (String token : tokens)
for (char c : token.toLowerCase().toCharArray())
if (c >= 'a' && c <= 'z')
letters[c - 'a']++;
// then print out counts:
for (int i = 0; i < 'z' - 'a' + 1; i++)
if (letters[i] > 0)
System.out.format("%s occurs %s time(s)\n", (char)('a' + i), letters[i]);
这看起来有点“低级”,但这是有效的Java代码(刚刚测试过!),它使用char
和int
s在算术运算上的可互换性。
答案 2 :(得分:1)
您可以创建一个for
循环,并为每个字母添加+1给数组或包含字母出现的HashMap
。
for(int i=0; i<= input.length(); i++){
mymap.put(input.charAt(i), myMap.get(input.charAt(i)+1));
}
但是你应该定义你的变量myMap
全局而不是你的main方法。
答案 3 :(得分:1)
您可以尝试Java 8 Stream API
除了Java流之外,您还应该了解Java Lambda Expressions
String input = "ANY String";
Map<String, Long> map = Arrays.stream(input.split("")) // Stream String
.map(String::toLowerCase) // All letters to lower case
.filter(letter -> !letter.equals(" ")) // Remove spaces
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
答案 4 :(得分:0)
你可以像这样简单地使用str.length()
:
System.out.println(key.length());