forked from mitchfry/SpellChecker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileParser.java
More file actions
73 lines (66 loc) · 1.72 KB
/
FileParser.java
File metadata and controls
73 lines (66 loc) · 1.72 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
65
66
67
68
69
70
71
72
73
import java.io.*;
import java.util.*;
//===================================================================
/**
* This class opens a TEXT file, reads it's contents and parses it one token at
* a time
*
* @author Mitch Fry
* @version 1.1
*/
public class FileParser {
// instance variables - replace the example below with your own
private String fileName;
private Scanner fileReader;
// ======================== Constructors =========================
/**
* Constructor for objects of class FileParser; default constructor override
*/
public FileParser() {
// Initialize instance variables
fileName = "";
}
/**
* Constructor for objects of class FileParser
*
* @param pFileName The name of the file to parse words from
*/
public FileParser(String pFileName) {
// Initialize instance variables
fileName = pFileName;
// Open the file and scanner
try {
fileRead();
} catch (Exception e) {
fileReader = null;
}
}
// ======================== General Methods ======================
/**
* Gets the next word in the text file
*
* @return Returns the next word/token in the text file
*/
public String getNextWord() {
// put your code here
if (fileReader.hasNext()) {
return fileReader.next();
}
else {
return null;
}
}
/**
* Method opens a file and creates the BufferedReader
*/
private void fileRead() throws Exception {
try {
File words = new File(fileName);
fileReader = new Scanner(words);
fileReader.useDelimiter("\\s\\s*|\\s*,\\s*|\\s*\\.\\s*|\\s*\"\\s*|\\s*\n\\s*|\\s*\r\\s*|\\s*\r\n\\s*|\\s*\n\r\\s*");
} catch (FileNotFoundException e) {
System.err.println("File: " + fileName + " not accessible");
System.err.println("Exception: " + e);
}
}
}