-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordGenerator.java
More file actions
49 lines (35 loc) · 1.04 KB
/
WordGenerator.java
File metadata and controls
49 lines (35 loc) · 1.04 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
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class WordGenerator {
private Scanner scanner;
private int words;
private int sentences;
// WordGenerator is a wrapper for a scanner that tracks the number of words and sentences scanned
// in
public WordGenerator(String filename) throws IOException {
this.scanner = new Scanner(new File(filename));
this.words = 0;
this.sentences = 0;
}
// All methods below are adaptations of regular scanner methods to fit this implementation
public boolean hasNext() {
return this.scanner.hasNext();
}
public String next() {
String word = scanner.next();
int last = word.length() - 1;
if (word.indexOf('.') == last || word.indexOf('?') == last || word.indexOf('!') == last) {
this.sentences++;
}
this.words++;
return word;
}
// These return the new fields added to the scanner class
public int getWordCount() {
return this.words;
}
public int getSentenceCount() {
return this.sentences;
}
}