Advertisement
thomasfelix

PBO 6

Nov 23rd, 2020 (edited)
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.StringTokenizer;
  4.  
  5. /**
  6.  * Class ini untuk parsing input user dan menginterpretasikan menjadi perintah
  7.  * "Adventure".
  8.  * Tiap kali dipanggil, class akan membaca line terminal dan interpretasi
  9.  * menjadi 2 kata perintah.
  10.  *
  11.  * @author Thomas Felix
  12.  * @version 23 November 2020
  13.  */
  14.  
  15. public class Parser
  16. {
  17.     private CommandWords commands;
  18.    
  19.     public Parser()
  20.     {
  21.         commands = new CommandWords();
  22.     }
  23.    
  24.     public Command getCommand()
  25.     {
  26.         String inputLine = "";
  27.         String word1;
  28.         String word2;
  29.        
  30.         System.out.print("> ");
  31.        
  32.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  33.        
  34.         try
  35.         {
  36.             inputLine = reader.readLine();
  37.         }
  38.        
  39.         catch (java.io.IOException exc)
  40.         {
  41.             System.out.println("There was an error during reading: " + exc.getMessage());
  42.         }
  43.        
  44.         StringTokenizer tokenizer = new StringTokenizer(inputLine);
  45.        
  46.         if (tokenizer.hasMoreTokens())
  47.         {
  48.             word1 = tokenizer.nextToken();
  49.         }
  50.        
  51.         else
  52.         {
  53.             word1 = null;
  54.         }
  55.        
  56.         if (tokenizer.hasMoreTokens())
  57.         {
  58.             word2 = tokenizer.nextToken();
  59.         }
  60.        
  61.         else
  62.         {
  63.             word2 = null;
  64.         }
  65.        
  66.         if (commands.isCommand(word1))
  67.         {
  68.             return new Command(word1, word2);
  69.         }
  70.        
  71.         else
  72.         {
  73.             return new Command(null, word2);
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement