mnaufaldillah

InputReader Tugas 5

Nov 16th, 2020 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. import java.util.HashSet;
  2. import java.util.Scanner;
  3. import java.util.Random;
  4. import java.util.ArrayList;
  5. import java.util.HashSet;
  6.  
  7. /**
  8.  * InputReader class to be used for read input entry.
  9.  *
  10.  * @author Muhammad Naufaldillah
  11.  * @version 16 November 2020
  12.  */
  13. public class InputReader
  14. {
  15.     private Scanner reader;
  16.    
  17.     public InputReader()
  18.     {
  19.         reader = new Scanner(System.in);
  20.     }
  21.    
  22.     /**
  23.      * Read a line of text from standard input (the text
  24.      * terminal). and return it as set of words.
  25.      *
  26.      * @return  A set of Strings, where each String is one of the
  27.      *          words typed by the user
  28.      */
  29.     public HashSet<String> getInput()
  30.     {
  31.        System.out.print("> "); // print prompt
  32.        String inputLine = reader.nextLine().trim().toLowerCase();
  33.        String[] wordArray = inputLine.split(" ");
  34.        
  35.        // add words from array into hashset
  36.        HashSet<String> words = new HashSet<String>();
  37.        for (String word : wordArray)
  38.        {
  39.            words.add(word);
  40.        }
  41.        
  42.        return words;
  43.     }
  44. }
  45.  
Add Comment
Please, Sign In to add comment