java - How to check duplicate from Strings given by user -


i writing code checking duplicate typed user. when user type second duplicate, program stop , warn user of duplication. logic put given words arraylist , check next given word in current arraylist if there or not.

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 word: ");             string word = reader.nextline();             words.add(word);             int = 0;             if (words.contains(words.get(i+1))) {                 system.out.println("you gave word " + words.get(i+1) + " twice");             }             i++;             break;         }     } } 

you need little reorganizing , logic check. if want stop when user tries add same word twice, stop , don't add @ list, won't have keep index @ all.

while (true) {     system.out.println("type word: ");     string word = reader.nextline();     if (words.contains(word)) {        system.out.println("you gave word " + word + " twice");        break;       }     words.add(word);    } 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -