Guest User

Untitled

a guest
Nov 23rd, 2020 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. import java.util.Scanner;
  2. /**
  3.  * Class Parser here.
  4.  *
  5.  */
  6. public class Parser
  7. {
  8.     private CommandWords commands;
  9.     private Scanner reader;
  10.    
  11.     /**
  12.      * Membuat sebuah parser untuk membaca dari terminal window.
  13.      */
  14.     public Parser()
  15.     {
  16.         commands = new CommandWords();
  17.         reader = new Scanner(System.in);
  18.     }
  19.    
  20.     /**
  21.      * @return Perintah selanjutnya dari user.
  22.      */
  23.     public Command getCommand()
  24.     {
  25.         String inputLine;
  26.         String word1 = null;
  27.         String word2 = null;
  28.        
  29.         System.out.print("-> ");
  30.        
  31.         inputLine = reader.nextLine();
  32.        
  33.         Scanner tokenizer = new Scanner(inputLine);
  34.         if(tokenizer.hasNext())
  35.         {
  36.            word1 = tokenizer.next();
  37.            if(tokenizer.hasNext())
  38.            {
  39.                word2 = tokenizer.next();
  40.            }
  41.         }
  42.        
  43.         if(commands.isCommand(word1))
  44.         {
  45.             return new Command(word1, word2);
  46.         }
  47.         else
  48.         {
  49.             return new Command(null, word2);
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment