Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.StringTokenizer;
- /**
- * Class ini untuk parsing input user dan menginterpretasikan menjadi perintah
- * "Adventure".
- * Tiap kali dipanggil, class akan membaca line terminal dan interpretasi
- * menjadi 2 kata perintah.
- *
- * @author Thomas Felix
- * @version 23 November 2020
- */
- public class Parser
- {
- private CommandWords commands;
- public Parser()
- {
- commands = new CommandWords();
- }
- public Command getCommand()
- {
- String inputLine = "";
- String word1;
- String word2;
- System.out.print("> ");
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- try
- {
- inputLine = reader.readLine();
- }
- catch (java.io.IOException exc)
- {
- System.out.println("There was an error during reading: " + exc.getMessage());
- }
- StringTokenizer tokenizer = new StringTokenizer(inputLine);
- if (tokenizer.hasMoreTokens())
- {
- word1 = tokenizer.nextToken();
- }
- else
- {
- word1 = null;
- }
- if (tokenizer.hasMoreTokens())
- {
- word2 = tokenizer.nextToken();
- }
- else
- {
- word2 = null;
- }
- if (commands.isCommand(word1))
- {
- return new Command(word1, word2);
- }
- else
- {
- return new Command(null, word2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement