如何检查用户

时间:2015-06-25 08:35:37

标签: java arraylist duplicates

我正在编写一个用于检查用户输入的重复的代码。当用户键入第二个副本时,程序将停止并警告用户重复。我的逻辑是,我会将给定的单词放到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;
        }
    }
}

6 个答案:

答案 0 :(得分:0)

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

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);  
 }

答案 1 :(得分:0)

尝试下面列出的内容

  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;
}

答案 2 :(得分:0)

首先。根据您的要求,无需使用迭代器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.
}

在您的示例中,用户只能输入一个单词,然后程序结束。

words.get(i+1)

将导致Exception,因为该元素永远不会存在。 你从元素0开始而不是1。

此外,我建议您使用Set,如果它是无效的情况,则使用相同的单词两次:

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

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

答案 3 :(得分:0)

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);
}

答案 4 :(得分:0)

尝试使用Set,它对于唯一值是特殊的。 add()方法return boolean:如果添加则返回true,否则返回false。 例如:

Set<String> example = new HashSet<String>();
String[] arr = {"a", "b", "c", "d", "a"};
for (int i = 0; i<5; i++)
{
    boolean res = example.add(arr[i]);
    System.out.println(res);
}

输出将是:

true
true
true
true
false

<强> 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;
            }
        }
    }
}

答案 5 :(得分:0)

你需要这样的东西:

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();
    }
}

如果您只想进行twice检查,请尝试使用以下代码段:

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();
    }
}