-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringHandler.java
More file actions
64 lines (56 loc) · 1.49 KB
/
StringHandler.java
File metadata and controls
64 lines (56 loc) · 1.49 KB
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
package icsi311;
public class StringHandler {
// Holds text file as string
private String document;
// index shows the position that has been parsed so far through the document.
private int index = 0;
private int size;
// Constructor for StringHandler class
public StringHandler(String s) {
document = s;
size = s.length();
}
/*
* char Peek(i) -looks “i” characters ahead and returns that character; doesn’t
* move the index
*/
public char Peek(int i) {
return document.charAt(index + i);
}
/*
* String PeekString(i) – returns a string of the next “i” characters but
* doesn’t move the index
*/
public String PeekString(int i) {
String f = "";
for (int n = 0; n < i; n++) {
char c = document.charAt(index + n);
f = f + c;
}
return f;
}
/*
* char GetChar() – returns the next character and moves the index
*/
public char GetChar() {
index++;
return document.charAt(index - 1);
}
/*
* void Swallow(i) – moves the index ahead “i” positions
*/
public void Swallow(int i) {
index = index + i;
}
/*
* boolean IsDone() – returns true if we are at the end of the document
*/
public boolean IsDone() {
try {
document.charAt(index);
} catch (Exception e) {
return true;
}
return false;
}
}