这应该很简单(我认为),但我无法做到正确......:|
任务如下:
询问用户输入的内容。必须将输入拆分为单个单词并放入数组中。应该计算所有单词。如果存在相等的单词,则它们在输出上得到“+1”。 最后,我想打印出来,并希望列表中有适当数量的计算单词。我的前两列是正确的,但是平等的话语让我头疼。如果发现一个单词是相同的,它在生成的列表中不能出现两次! :
我是一个完整的JAVA新手,所以请对代码判断表示友好。 ;)
到目前为止,这是我的代码:
package MyProjects;
import javax.swing.JOptionPane;
public class MyWordCount {
public static void main(String[] args) {
//User input dialog
String inPut = JOptionPane.showInputDialog("Write som text here");
//Puts it into an array, and split it with " ".
String[] wordList = inPut.split(" ");
//Print to screen
System.out.println("Place:\tWord:\tCount: ");
//Check & init wordCount
int wordCount = 0;
for (int i = 0; i < wordList.length; i++) {
for (int j = 0; j < wordList.length; j++){
//some code here to compare
//something.compareTo(wordList) ?
}
System.out.println(i + "\t" + wordList[i]+ "\t" + wordCount[?] );
}
}
}
答案 0 :(得分:6)
您可以使用Hashmap来执行此操作。 Hashmap存储键值对,每个键必须是唯一的。
因此,在您的情况下,一个键将是您已拆分的字符串中的一个字,值将是它的计数。
将输入拆分为单词并将其放入字符串数组后,将第一个单词(作为键)放入Hashmap中,将1作为值。对于每个后续单词,您可以使用函数containsKey()将该单词与Hashmap中的任何现有键进行匹配。如果它返回true,则将该键的值(count)递增1,否则将word和1作为新的键值对放入Hashmap。
答案 1 :(得分:2)
因此,为了比较两个字符串,您可以:
String stringOne = "Hello";
String stringTwo = "World";
stringOne.compareTo(stringTwo);
//Or you can do
stringTwo.compareTo(stringOne);
您不能像在评论中那样将String与String数组进行比较。你必须在这个字符串数组中取一个元素,并进行比较(So stringArray [elementNumber])。
对于计算有多少单词,如果要确定重复单词的数量,则需要一个整数数组(所以创建一个新的int [])。 new int []中的每个位置都应对应于单词数组中的单词。这将允许您计算重复单词的次数。
答案 2 :(得分:1)
import java.util.ArrayList;
import java.util.regex.PatternSyntaxException;
import javax.swing.JOptionPane;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
//Print to screen
System.out.println("Place:\tWord:\tCount: ");
//User input dialog
String inPut = JOptionPane.showInputDialog("Write som text here");
//Puts it into an array, and split it with " ".
String[] wordList;
try{
wordList = inPut.split(" ");
}catch(PatternSyntaxException e) {
// catch the buggy!
System.out.println("Ooops.. "+e.getMessage());
return;
}catch(NullPointerException n) {
System.out.println("cancelled! exitting..");
return;
}
ArrayList<String> allWords = new ArrayList<String>();
for(String word : wordList) {
allWords.add(word);
}
// reset unique words counter
int uniqueWordCount = 0;
// Remove all of the words
while(allWords.size() > 0) {
// reset the word counter
int count = 0;
// get the next word
String activeWord = allWords.get(0);
// Remove all instances of this word
while(doesContainThisWord(allWords, activeWord)) {
allWords.remove(activeWord);
count++;
}
// increase the unique word count;
uniqueWordCount++;
// print result.
System.out.println(uniqueWordCount + "\t" + activeWord + "\t" + count );
}
}
/**
* This function returns true if the parameters are not null and the array contains an equal string to newWord.
*/
public static boolean doesContainThisWord(ArrayList<String> wordList, String newWord) {
// Just checking...
if (wordList == null || newWord == null) {
return false;
}
// Loop through the list of words
for (String oldWord : wordList) {
if (oldWord.equals(newWord)) {
// gotcha!
return true;
}
}
return false;
}
}
答案 3 :(得分:1)
这是一个使用WordInfo对象映射的解决方案,它记录文本中单词的位置并将其用作计数。 LinkedHashMap保留了从第一次输入时开始的键的顺序,因此只需通过键遍历就可以“按照外观顺序进行转换”
通过将所有键存储为小写但将原始案例存储在WordInfo对象中,可以使此案例不区分大小写,同时保留第一个外观的大小写。或者只是将所有单词转换为小写并保留它。
您可能还想考虑在拆分之前从第一个文本中删除所有,
/ .
/ "
等,但无论如何,您永远不会完美。
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.JOptionPane;
public class MyWordCount {
public static void main(String[] args) {
//User input dialog
String inPut = JOptionPane.showInputDialog("Write som text here");
Map<String,WordInfo> wordMap = new LinkedHashMap<String,WordInfo>();
//Puts it into an array, and split it with " ".
String[] wordList = inPut.split(" ");
for (int i = 0; i < wordList.length; i++) {
String word = wordList[i];
WordInfo wi = wordMap.get(word);
if (wi == null) {
wi = new WordInfo();
}
wi.addPlace(i+1);
wordMap.put(word,wi);
}
//Print to screen
System.out.println("Place:\tWord:\tCount: ");
for (String word : wordMap.keySet()) {
WordInfo wi = wordMap.get(word);
System.out.println(wi.places() + "\t" + word + "\t" + wi.count());
}
}
}
WordInfo类:
import java.util.ArrayList;
import java.util.List;
public class WordInfo {
private List<Integer> places;
public WordInfo() {
this.places = new ArrayList<>();
}
public void addPlace(int place) {
this.places.add(place);
}
public int count() {
return this.places.size();
}
public String places() {
if (places.size() == 0)
return "";
String result = "";
for (Integer place : this.places) {
result += ", " + place;
}
result = result.substring(2, result.length());
return result;
}
}
答案 4 :(得分:0)
感谢您试图帮助我。 - 这就是我最终做的事情:
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class MyWordCount {
public static void main(String[] args) {
// Text in
String inText = JOptionPane.showInputDialog("Write some text here");
// Puts it into an array, and splits
String[] wordlist = inText.split(" ");
// Text out (Header)
System.out.println("Place:\tWord:\tNo. of Words: ");
// declare Arraylist for words
ArrayList<String> wordEncounter = new ArrayList<String>();
ArrayList<Integer> numberEncounter = new ArrayList<Integer>();
// Checks number of encounters of words
for (int i = 0; i < wordlist.length; i++) {
String word = wordlist[i];
// Make everything lowercase just for ease...
word = word.toLowerCase();
if (wordEncounter.contains(word)) {
// Checks word encounter - return index of word
int position = wordEncounter.indexOf(word);
Integer number = numberEncounter.get(position);
int number_int = number.intValue();
number_int++;
number = new Integer(number_int);
numberEncounter.set(position, number);
// Number of encounters - add 1;
} else {
wordEncounter.add(word);
numberEncounter.add(new Integer(1));
}
}
// Text out (the list of words)
for (int i = 0; i < wordEncounter.size(); i++) {
System.out.println(i + "\t" + wordEncounter.get(i) + "\t"
+ numberEncounter.get(i));
}
}
}