Java 如何从用户提供的字符串中检查重复项

Java 如何从用户提供的字符串中检查重复项,java,arraylist,duplicates,Java,Arraylist,Duplicates,我正在编写一个代码,用于检查用户键入的副本。当用户键入第二个副本时,程序将停止并警告用户该副本。我的逻辑是将给定的单词放入ArrayList,然后检查当前ArrayList中的下一个给定单词是否已经存在 public class RecurringWord { public static void main(String[] args) { Scanner reader = new Scanner(System.in); ArrayList<Str

我正在编写一个代码,用于检查用户键入的副本。当用户键入第二个副本时,程序将停止并警告用户该副本。我的逻辑是将给定的单词放入
ArrayList
,然后检查当前
ArrayList
中的下一个给定单词是否已经存在

public class RecurringWord {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        ArrayList<String> words = new ArrayList<String>();
        while (true) {
            System.out.println("Type a word: ");
            String word = reader.nextLine();
            words.add(word);
            int i = 0;
            if (words.contains(words.get(i+1))) {
                System.out.println("You gave the word " + words.get(i+1) + " twice");
            }
            i++;
            break;
        }
    }
}
公共类重现词{
公共静态void main(字符串[]args){
扫描仪阅读器=新扫描仪(System.in);
ArrayList words=新的ArrayList();
while(true){
System.out.println(“键入单词:”);
String word=reader.nextLine();
添加(word);
int i=0;
if(words.contains(words.get(i+1))){
System.out.println(“您给出了单词”+words.get(i+1)+“两次”);
}
i++;
打破
}
}
}

您需要进行一些重新组织和逻辑检查。如果您想在用户尝试添加同一个单词两次时停止,然后停止并根本不将其添加到列表中,那么您就不必保留任何索引

while (true) {
    System.out.println("Type a word: ");
    String word = reader.nextLine();
    if (words.contains(word)) {
       System.out.println("You gave the word " + word + " twice");
       break;
      }
    words.add(word);  
 }

试试下面列出的东西

  ArrayList<String> words = new ArrayList<String>();
   while (true) {
    System.out.println("Type a word: ");
    String word = reader.nextLine();

    if (words.contains(word)) {
        System.out.println("You gave the word " + words.get(i+1) + " twice");
    }else{
       words.add(word);

    break;
}
ArrayList words=new ArrayList();
while(true){
System.out.println(“键入单词:”);
String word=reader.nextLine();
if(words.contains(word)){
System.out.println(“您给出了单词”+words.get(i+1)+“两次”);
}否则{
添加(word);
打破
}

首先。对于您的需求,无需使用迭代器
i
。 其他信息见评论:

Scanner reader = new Scanner(System.in);

Set<String> words = new HashSet<>();
while (true) {
    System.out.println("Type a word: ");
    String word = reader.nextLine();
    if (words.contains(word)) {
        System.out.println("You gave the word " + words + " twice");
        break; // end the programm if the word exists twice
    }
    words.add(word); // add the new word after the check.
}
将导致
异常
,因为该元素将永远不存在。 从元素0而不是1开始

此外,如果两次使用同一单词是无效的,我建议您使用集合:

Set<String> words = new HashSet<>();
Set words=newhashset();

Set
永远不会包含两个相等的字符串。

尝试使用Set,它对唯一值是特殊的。add()方法返回布尔值:如果添加,则返回true;如果不添加,则返回false。
while (true) {
        System.out.println("Type a word: ");
        String word = reader.nextLine();
        if (words.contains(word)) {
            System.out.println("You gave the word " + word + " twice");
            break;
        }
        //This is only reached when `word` is new. 
        //Add it to the list to keep track of it for later possible duplicates
        words.add(word);
}
例如:

UPD

你的例子是:

public class RecurringWord {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        Set<String> words = new HashSet<String>();
        while (true) {
            System.out.println("Type a word: ");
            String word = reader.nextLine();
            if(!words.add(word));{
                System.out.println("You gave the word " + word+" twice");
                break;
            }
        }
    }
}
公共类重现词{
公共静态void main(字符串[]args){
扫描仪阅读器=新扫描仪(System.in);
Set words=新HashSet();
while(true){
System.out.println(“键入单词:”);
String word=reader.nextLine();
如果(!words.add(word)){
System.out.println(“您给了单词“+word+”两次”);
打破
}
}
}
}

您需要这样的东西:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class RecurringWord {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        Map<String, Integer> countMap = new HashMap<String, Integer>();
        System.out.println("Type a word: ");
        String word = reader.nextLine();
        String[] words = word.split(" ");
        for (int i = 0; i < words.length; i++) {
            if (countMap.containsKey(words[i])) {
                countMap.put(words[i], countMap.get(words[i]) + 1);
            } else {
                countMap.put(words[i], 1);
            }
        }
        for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
            if (entry.getValue() > 1) {
                System.out.println("You gave the word " + entry.getKey()
                        + " : " + entry.getValue() + " many times");
            }
        }
        reader.close();
    }
}

用迭代器遍历列表…然后检查重复项…再试一次…这将对您有所帮助..因为您是javalist新手。contains(word)区分大小写…如果您不区分大小写,请使用string.equalsIgnoreCase(string)你为什么不把它们放在一个
集合中
?使用哈希集来过滤重复项。谢谢你们的快速回复。我目前正在阅读所有内容并找出高级内容。正如我所说,我是Java新手,大约有一周了。感谢你们的帮助。你的答案非常容易理解,适合我的水平。这对我很有帮助很多!
true
true
true
true
false
public class RecurringWord {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        Set<String> words = new HashSet<String>();
        while (true) {
            System.out.println("Type a word: ");
            String word = reader.nextLine();
            if(!words.add(word));{
                System.out.println("You gave the word " + word+" twice");
                break;
            }
        }
    }
}
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class RecurringWord {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        Map<String, Integer> countMap = new HashMap<String, Integer>();
        System.out.println("Type a word: ");
        String word = reader.nextLine();
        String[] words = word.split(" ");
        for (int i = 0; i < words.length; i++) {
            if (countMap.containsKey(words[i])) {
                countMap.put(words[i], countMap.get(words[i]) + 1);
            } else {
                countMap.put(words[i], 1);
            }
        }
        for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
            if (entry.getValue() > 1) {
                System.out.println("You gave the word " + entry.getKey()
                        + " : " + entry.getValue() + " many times");
            }
        }
        reader.close();
    }
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class RecurringWord {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        System.out.println("Type a word: ");
        String word = reader.nextLine();
        String[] words = word.split(" ");
        List<String> wordsList = new ArrayList<>();
        for (int i = 0; i < words.length; i++) {
            if (wordsList.contains(words[i])) {
                System.out
                        .println("You gave the word " + words[i] + " : twice");
            } else {
                wordsList.add(words[i]);
            }
        }
        reader.close();
    }
}