java 如何在java中计算字符串数组中的单词?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14936107/
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
How to count words in array of strings in java?
提问by user1835208
I am learning about arrays and I wanted to make a program count words. Given: String myWords = {"soon; hi; also; soon; job; also"};
, I have to create a method like countWrods(myWords);
The printed result should be the words printed alphabetical order, the number of unique words and total words.
here is my code:
我正在学习数组,我想让一个程序计算单词。鉴于:String myWords = {"soon; hi; also; soon; job; also"};
,我必须创建一个方法,如countWrods(myWords);
打印结果应该是按字母顺序打印的单词,唯一单词的数量和总单词数。这是我的代码:
public class Words {
public static void main(String[] args){
String[] myWords = {"soon; hi; also; soon; job; mother; job; also; soon; later"};
Words myW= new Words();
myW.countWords();
System.out.println("\tWords \tFreq");
}
public static String[] countWords(myWords){
for (int i=0; i<myWords.length; i++){
String temp = myWords[i];
//System.out.println(temp + " ");
for(int j=i+1; j<myWords.length; j++){
String temp2= myWords[j];
System.out.println("No. of unique words: " );
}
}
}
}
What should I do next?
我接下来该怎么做?
回答by Shubham
import java.io.*;
import java.util.*;
public class Count_Words_Scan
{
void main()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A STRING ");
String str = br.readLine();
str= str.toLowerCase();
int c=0;
Scanner sc = new Scanner(str);
while(sc.hasNext())
{
sc.next();
c++;
}
System.out.println("NO.OF WORDS = "+c);
}
}
Input: the word counter
Output: NO.OF WORDS = 3
输入:字计数器
输出:NO.OF WORDS = 3
回答by Ivaylo Strandjev
回答by amicngh
I am assuming you want to count the words in a string .
我假设您想计算 string 中的单词数。
String: "soon hi also soon job mother job also soon later"
字符串:"soon hi also soon job mother job also soon later"
public class Words {
Map<String , Integer> dictionary=new HashMap<String,Integer>();
public static void main(String[] args) {
String myWords = "soon hi also soon job mother job also soon later";
Words myW = new Words();
String[] array=myWords.split("\s+");
myW.countWords(array);
System.out.println(myW.dictionary);
}
private void countWords(String[] myWords) {
for(String s:myWords){
if(dictionary.containsKey(s))
dictionary.put(s, dictionary.get(s)+1);
else
dictionary.put(s, 1);
}
}
}
O/P : {mother=1, later=1, job=2, hi=1, also=2, soon=3}
O/P : {mother=1, later=1, job=2, hi=1, also=2, soon=3}
回答by Boris the Spider
First you need to split your String
, presumably on ";" - then you can whack that into a TreeSet
to sort it and make then words unqiue. Add a counter to count the total words. You could also use a TreeMap
to keep a count of each word, override the put method on the map to aggregate as you go...
首先,您需要拆分您的String
,大概是在“;”上 - 然后你可以TreeSet
把它打成一个来排序,然后让单词unqiue。添加一个计数器来计算总字数。您还可以使用 aTreeMap
来记录每个单词的数量,覆盖地图上的 put 方法以在您进行时进行聚合...
final String myString = {"soon; hi; also; soon; job; mother; job; also; soon; later"};
final String[] myStrings = myString.split(";");
final Map<String, Integer> myStringMap = new TreeMap<>(){
@override
public String put(final String key, final Integer value) {
if(contains(key)) {
return put(key, get(key) + 1);
} else {
return put(key, 1);
}
}
};
for(final String string : myStrings) {
myStringMap.put(string.trim(), 1);
}
Now myStringMap.size()
is the number of unique words, myStringMap.keys()
is a alphabetically sorted Set
of all unquie words and if you want the total you just need to add up the values:
现在myStringMap.size()
是唯一词的数量,myStringMap.keys()
是按字母顺序排列Set
的所有 unquie 词,如果您想要总数,您只需要将值相加:
int totalWords = 0;
for(final Integer count : myStringMap.values()) {
totalWorks += count;
}