fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. //Let's make our objects that will be used in the loop
  13. Scanner reader = new Scanner(System.in);
  14. ArrayList<String> words = new ArrayList<String>();
  15.  
  16. while (true) {
  17. String input = reader.nextLine();
  18.  
  19. //Now that we have the input, we can check if it's already in the list:
  20. if (words.contains(input)) {
  21. //If it is, then we need to break out of the loop. We can also give the user the error msg here
  22. System.out.println("You gave the word " + input +" twice");
  23. break;
  24. } else {
  25. //If we got this far, it means the list doesn't contain our input, so we can add it to the list and move on
  26. words.add(input);
  27. }
  28. }
  29.  
  30. //Let's print out the resulting array to see what happened:
  31. for (String word : words) {
  32. System.out.println(word);
  33. }
  34. }
  35. }
Success #stdin #stdout 0.16s 321344KB
stdin
asd
bfe
qwe
asd
ggg
stdout
You gave the word asd twice
asd
bfe
qwe