Files
prog-intro-2025/java/wspp/WordScanner.java
me d8154da7a9
Some checks failed
Fast Reverse Tests / test (push) Failing after 4s
Reverse Tests / test (push) Failing after 4s
Sum Tests / test (push) Failing after 4s
Word Stat Tests / test (push) Failing after 4s
Word Stat++ Tests / test (push) Failing after 4s
add solution for hw6:3233
2026-02-02 14:27:24 +03:00

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;
}
}