这应该是非常简单的(我认为),但我只是不能正确地做到......:|如何在JAVA中检查字符串数组中的相等字
任务如下:
询问用户一些输入。输入必须分成单个单词并放入一个数组中。所有单词都应该被计算在内如果存在相同的单词,则它们会在输出中获得“+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[?]);
}
}
}
也许我应该改变两个for-loops用for-each循环insted? – CustomCase
使用'列表 words = Arrays.asList(inPut.split(“”));'然后使用'Collections.frequency(words,word)'来确定单词的实例数量。 –
oldrinb
这还没有得到您的满意吗?你需要更多信息吗? – Link19