Files
prog-intro-2025/java/wspp/WordScanner.java
me 380611c4df
All checks were successful
Fast Reverse Tests / test (push) Successful in 24s
Reverse Tests / test (push) Successful in 8s
Sum Tests / test (push) Successful in 9s
Word Stat Tests / test (push) Successful in 8s
add solutions for hw6:Base
2026-02-02 13:25:10 +05:00

57 lines
1.4 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;
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();
pos = 0;
return line != null;
}
private boolean isWordChar(char c) {
return Character.isLetter(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 void close() throws IOException {
br.close();
}
}