Advertisement
Masovski

[Java Basics][Collections-HW] 10. Extract All Unique Words

May 24th, 2014
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class ExtractAllUniqueWords {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.        
  9.         String input = sc.nextLine().toLowerCase();
  10.         String[] words = input.split("\\W+");
  11.        
  12.         ArrayList<String> finalWords = new ArrayList<>();
  13.        
  14.         for (int i = 0; i < words.length; i++) {
  15.             if(!finalWords.contains(words[i])) {
  16.                 finalWords.add(words[i]);
  17.             }
  18.         }
  19.         finalWords.sort(null);
  20.        
  21.         for (String str : finalWords) {
  22.             System.out.print(str + " ");
  23.         }
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement