Java 使用哈希图创建文本的字数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20849512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create word count of text using hashmap
提问by f3d0r
I am trying to create a program as a tutorial for myself for hashmaps. I ask the user into text and try to split it into hashmaps and then increase the count if the word repeats. This is my program:
我正在尝试为自己创建一个程序作为哈希图的教程。我要求用户输入文本并尝试将其拆分为哈希图,然后在单词重复时增加计数。这是我的程序:
import java.util.*;
import java.lang.*;
import javax.swing.JOptionPane;
import java.io.*;
public class TestingTables
{
public static void main(String args[])
{
{
String s = JOptionPane.showInputDialog("Enter any text.");
String[] splitted = s.split(" ");
HashMap hm = new HashMap();
int x;
for (int i=0; i<splitted.length ; i++) {
hm.put(splitted[i], i);
System.out.println(splitted[i] + " " + i);
if (hm.containsKey(splitted[i])) {
x = ((Integer)hm.get(splitted[i])).intValue();
hm.put(splitted[i], new Integer(x+1)); }
}
}
}
}
When I input "random random random", I get: random 0 random 1 random 2
当我输入“random random random”时,我得到:random 0 random 1 random 2
What do I need to change so I get: random 3 Also, do I need to use an iterator to print out the hashmap, or is what I used OK?
我需要更改什么才能得到: random 3 另外,我是否需要使用迭代器来打印哈希图,或者我使用的是否可以?
采纳答案by peter.petrov
Your initialization is wrong hm.put(splitted[i], i)
.
You should initialize to 0 or to 1 (to count, not to index).
你的初始化是错误的hm.put(splitted[i], i)
。您应该初始化为 0 或 1(计数,而不是索引)。
So do this loop first.
所以先做这个循环。
for (int i = 0; i < splitted.length; i++) {
if (!hm.containsKey(splitted[i])) {
hm.put(splitted[i], 1);
} else {
hm.put(splitted[i], (Integer) hm.get(splitted[i]) + 1);
}
}
Then just do one more loop (iterate through the keys of the HashMap) and print the counts out.
然后再执行一次循环(遍历 HashMap 的键)并打印出计数。
for (Object word : hm.keySet()){
System.out.println(word + " " + (Integer) hm.get(word));
}
回答by Vannens
import java.util.*;
import java.lang.*;
import javax.swing.JOptionPane;
import java.io.*;
public class TestingTables
{
public static void main(String args[])
{
{
String s = JOptionPane.showInputDialog("Enter any text.");
String[] splitted = s.split(" ");
Map<String, Integer> hm = new HashMap<String, Integer>();
int x;
for (int i=0; i<splitted.length ; i++) {
if (hm.containsKey(splitter[i])) {
int cont = hm.get(splitter[i]);
hm.put(splitter[i], cont + 1)
} else {
hm.put(splitted[i], 1);
}
}
}
}
Your Map declaration is wrong, remember the correct way to implement a Map.
您的 Map 声明是错误的,请记住实现 Map 的正确方法。
回答by IS_EV
This should work, its a pretty simple implementation..
这应该有效,它是一个非常简单的实现..
Map<String, Integer> hm = new HashMap<String, Integer>();
int x;
for (int i = 0; i < splitted.length; i++) {
if (hm.containsKey(splitted[i])) {
x = hm.get(splitted[i]);
hm.put(splitted[i], x + 1);
} else {
hm.put(splitted[i], 1);
}
}
for (String key : hm.keySet()) {
System.out.println(key + " " + hm.get(key));
}