75 lines
1.9 KiB
Java
75 lines
1.9 KiB
Java
package wspp;
|
|
|
|
import java.io.*;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
public class WordScanner {
|
|
private BufferedReader br;
|
|
private String line = null;
|
|
private int pos = 0;
|
|
private int lineNumber = 0;
|
|
|
|
public WordScanner(String fileName) throws IOException {
|
|
br = new BufferedReader(new InputStreamReader(
|
|
new FileInputStream(fileName), StandardCharsets.UTF_8));
|
|
}
|
|
|
|
private boolean hasNextLine() throws IOException {
|
|
if (line != null && pos < line.length()) return true;
|
|
line = br.readLine();
|
|
if (line != null) {
|
|
lineNumber++;
|
|
}
|
|
pos = 0;
|
|
return line != null;
|
|
}
|
|
|
|
private boolean isWordChar(char c) {
|
|
return Character.isLetter(c) || Character.isDigit(c) ||
|
|
c == '\'' || c == '$' || c == '_' ||
|
|
Character.getType(c) == Character.DASH_PUNCTUATION;
|
|
}
|
|
|
|
public boolean hasNextWord() throws IOException {
|
|
while (hasNextLine()) {
|
|
while (pos < line.length() && !isWordChar(line.charAt(pos))) {
|
|
pos++;
|
|
}
|
|
if (pos < line.length()) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public String nextWord() {
|
|
while (pos < line.length() && !isWordChar(line.charAt(pos))) {
|
|
pos++;
|
|
}
|
|
|
|
int start = pos;
|
|
while (pos < line.length() && isWordChar(line.charAt(pos))) {
|
|
pos++;
|
|
}
|
|
|
|
return line.substring(start, pos).toLowerCase();
|
|
}
|
|
|
|
public int getLineNumber() {
|
|
return lineNumber;
|
|
}
|
|
|
|
public void close() throws IOException {
|
|
br.close();
|
|
}
|
|
|
|
public void reset(String fileName) throws IOException {
|
|
br.close();
|
|
br = new BufferedReader(new InputStreamReader(
|
|
new FileInputStream(fileName), StandardCharsets.UTF_8));
|
|
line = null;
|
|
pos = 0;
|
|
lineNumber = 0;
|
|
}
|
|
}
|