Advertisement
dimipan80

10. Extract All Unique Words

Sep 13th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. /* At the first line at the console you are given a piece of text.
  2.  * Extract all words from it and print them in alphabetical order.
  3.  * Consider each non-letter character as word separator.
  4.  * Take the repeating words only once. Ignore the character casing.
  5.  * Print the result words in a single line, separated by spaces. */
  6.  
  7. import java.util.Scanner;
  8. import java.util.TreeSet;
  9.  
  10. public class _10_ExtractAllUniqueWords {
  11.  
  12.     public static void main(String[] args) {
  13.         // TODO Auto-generated method stub
  14.         Scanner scan = new Scanner(System.in);
  15.         System.out.println("Enter your text on single line:");
  16.         String inputLine = scan.nextLine().toLowerCase();
  17.  
  18.         String[] textStr = inputLine.split("[^a-z]+");
  19.         TreeSet<String> extractWords = new TreeSet<>();
  20.         for (String word : textStr) {
  21.             extractWords.add(word);
  22.         }
  23.  
  24.         System.out.println("The all Extract words in alphabetical order are:");
  25.         for (String string : extractWords) {
  26.             System.out.print(string + " ");
  27.         }
  28.     }
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement