-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.java
110 lines (96 loc) · 2.17 KB
/
Parser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Scanner;
/**
* Write a description of class Parser here.
*
* @author Jerrett Fowler
* @version 1.0 (August 2013)
*/
public class Parser
{
// instance variables - replace the example below with your own
private CommandWords commands;
private String input;
private String w1;
private String theRest;
/**
* Constructor for objects of class Parser
*/
public Parser()
{
commands = new CommandWords();
}
/**
* Get commands
*
* @return the command
*/
public Command getCommand()
{
input = new String("");
//prompt
System.out.print(">> ");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try
{
input = reader.readLine();
}
catch(java.io.IOException exc)
{
System.out.println("Error reading: " + exc.getMessage());
}
StringTokenizer tokenizer = new StringTokenizer(input);
if(tokenizer.hasMoreTokens())
{
w1 = tokenizer.nextToken(); //Word 1
}
else
{
w1 = null;
}
//store the rest as a string if there are more
while(tokenizer.hasMoreTokens())
{
theRest += " " + tokenizer.nextToken();
}
/*
if(tokenizer.hasMoreTokens())
{
w2 = tokenizer.nextToken(); //Word 2
}
else
{
w2 = null;
}
*/
Command command = commands.getCommand(w1);
if(command != null)
{
command.setSecondWord(theRest);
}
return command;
}
/**
* Get word 1 as string
*/
public String getW1()
{
return w1;
}
/**
* Get word 2 as string
*/
public String getW2()
{
return theRest;
}
/**
* Get CommandWords
*/
public CommandWords getCommandWords()
{
return commands;
}
}