0

I was wondering how to check user input that was previously stored in an arraylist.

For example, I am asking a user to enter in letters for which i store into an arraylist. I need to make sure they don't guess the same letter again but I am not sure how. Any tips are appreciated.

    Scanner scan = new Scanner(System.in);
    ArrayList<String>guesses=new ArrayList<String>();

    System.out.println("Enter a letter");
    String userInput = scan.next();
    guesses.add(userInput);
2
  • Iterate over the list and look if the value is already in the list. Commented Apr 13, 2015 at 5:51
  • @Jens yeah i tried but I'm not sure how to avoid checking the current input. Like say i am currently at index 3 but i want to check 1 and 2 Commented Apr 13, 2015 at 5:58

3 Answers 3

5

That functionality would work better with a HashSet, since searching for an existing value in a HashSet is faster than in an ArrayList.

Scanner scan = new Scanner(System.in);
Set<String> guesses=new HashSet<String>();

System.out.println("Enter a letter");
String userInput = scan.next();
if (guesses.contains(userInput)) {
    // the user repeated a guess
} else {
    guesses.add(userInput);
}

Of course, if you only accept one character at a time, you can use a simple array to find if a guess is repeated :

Scanner scan = new Scanner(System.in);
boolean[] guesses=new boolean[26];

System.out.println("Enter a letter");
String userInput = scan.next();
char c = userInput.charAt(0);
if (c >= 'a' && c <= 'z' && !guesses[c-'a']) {
    guesses[c-'a']=true;
    // this is a valid guess
} else {
    // this is an invalid guess
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!! I haven't learned HashSet in class so I couldn't have used it but the second method works perfectly!
1

If you don't want any duplicates then I would suggest that you use a HashSet. To see if the guess was previously done, just call the contains() method.

if (!guesses.contains(userInput)) {
    guesses.add(userInput);
} else {
    ...
}

1 Comment

Another good thing about Set is that it tells you if it could add the item to the set or not, so you can use if (!guesses.add(userInput)) { /* already in the set */ } instead. But your answer is still correct.
1

Firstly, as a good practice you should always code to an interface thus:

ArrayList<String>guesses=new ArrayList<String>();

should be changed to:

List<String>guesses=new ArrayList<String>();

Now coming back to your question, there are two options:

  1. instead of List use Set, as it avoids duplicates, you can be sure to have unique values (recommended)

  2. after every input execute

    if (!guesses.contains(userInput)) 
        guesses.add(userInput);
    else 
        // if you want you can intimate the user of a duplicate value
    

1 Comment

Thanks for the tip! I'm just learning this so it's still all new to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.