我对这个项目有疑问。基本前提是用户输入一个短语,它应该找到任何重复的单词和有多少单词。
我的问题是多次输入一个单词,例如...... 你好你好你好你好你好
该输出将是;
"There are 2 duplicates of the word "hello" in the phrase you entered."
"There are 1 duplicates of the word "hello" in the phrase you entered."
这似乎只发生在这种情况下。如果我输入一个随机输入多个单词的随机短语,它会显示正确的答案。我认为这个问题与删除重复的单词以及它遍历短语的次数有关,但我无法绕过它。我已经在任何地方添加了打印行,并且改变了它以各种方式迭代的次数,我在Java Visualizer中通过它并且仍然无法找到确切的问题。非常感谢任何帮助!
这是我在线Java课程的作业,但它只适用于学习/练习,不适合我的专业。虽然只是帮助,但我并不是在寻找答案。
public class DuplicateWords {
public static void main(String[] args) {
List<String> inputList = new ArrayList<String>();
List<String> finalList = new ArrayList<String>();
int duplicateCounter;
String duplicateStr = "";
Scanner scan = new Scanner(System.in);
System.out.println("Enter a sentence to determine duplicate words entered: ");
String inputValue = scan.nextLine();
inputValue = inputValue.toLowerCase();
inputList = Arrays.asList(inputValue.split("\\s+"));
finalList.addAll(inputList);
for(int i = 0; i < inputList.size(); i++) {
duplicateCounter = 0;
for(int j = i + 1; j < finalList.size(); j++) {
if(finalList.get(i).equalsIgnoreCase(finalList.get(j))
&& !finalList.get(i).equals("!") && !finalList.get(i).equals(".")
&& !finalList.get(i).equals(":") && !finalList.get(i).equals(";")
&& !finalList.get(i).equals(",") && !finalList.get(i).equals("\"")
&& !finalList.get(i).equals("?")) {
duplicateCounter++;
duplicateStr = finalList.get(i).toUpperCase();
}
if(finalList.get(i).equalsIgnoreCase(finalList.get(j))) {
finalList.remove(j);
}
}
if(duplicateCounter > 0) {
System.out.printf("There are %s duplicates of the word \"%s\" in the phrase you entered.", duplicateCounter, duplicateStr);
System.out.println();
}
}
}
}
根据我编辑代码的一些建议,但我不确定我是否朝着正确的方向前进
String previous = "";
for(Iterator<String> i = inputList.iterator(); i.hasNext();) {
String current = i.next();
duplicateCounter = 0;
for(int j = + 1; j < finalList.size(); j++) {
if(current.equalsIgnoreCase(finalList.get(j))
&& !current.equals("!") && !current.equals(".")
&& !current.equals(":") && !current.equals(";")
&& !current.equals(",") && !current.equals("\"")
&& !current.equals("?")) {
duplicateCounter++;
duplicateStr = current.toUpperCase();
}
if(current.equals(previous)) {
i.remove();
}
}
if(duplicateCounter > 0) {
System.out.printf("There are %s duplicates of the word \"%s\" in the phrase you entered.", duplicateCounter, duplicateStr);
System.out.println();
}
}
答案 0 :(得分:1)
我首先要用每个单词填充Map<String, Integer>
;每次遇到单词时都会增加Integer
。像
String inputValue = scan.nextLine().toLowerCase();
String[] words = inputValue.split("\\s+");
Map<String, Integer> countMap = new HashMap<>();
for (String word : words) {
Integer current = countMap.get(word);
int v = (current == null) ? 1 : current + 1;
countMap.put(word, v);
}
然后,您可以迭代Map
entrySet
并显示计数大于key
的每个word
(1
)。像,
String msgFormat = "There are %d duplicates of the word \"%s\" in "
+ "the phrase you entered.%n";
for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
if (entry.getValue() > 1) {
System.out.printf(msgFormat, entry.getValue(), entry.getKey());
}
}
答案 1 :(得分:1)
您的代码问题在于,当您删除某个项目时,仍会增加索引,因此您可以跳过下一个项目。在缩写形式中,您的代码是:
for (int j = i + 1; j < finalList.size(); j++) {
String next = finalList.get(i);
if (some test on next)
finalList.remove(next);
}
在调用remove之后,“next”项目将位于相同的索引处,因为直接删除项目会导致右边的所有项目被洗牌1个位置以填补空白。要修复,您应该在删除后添加以下行:
i--;
这可以解决您的问题,但是,有一种更简洁的方法:
String previous = "";
for (Iterator<String> i = inputList.iterator(); i.hasNext();) {
String current = i.next();
if (current.equals(previous)) {
i.remove(); // removes current item
}
previous = current;
}
inputList
现在删除了所有相邻的重复项。
删除所有重复项:
List<String> finalList = inputList.stream().distinct().collect(Collectors.toList());
如果您喜欢疼痛,请“手动”:
Set<String> duplicates = new HashSet<>(); // sets are unique
for (Iterator<String> i = inputList.iterator(); i.hasNext();)
if (!duplicates.add(i.next())) // add returns true if the set changed
i.remove(); // removes current item
答案 2 :(得分:0)
在将inputList
添加到finalList
之前,请从inputList
中删除所有重复的项目。