Cześć, mam plik tekstowy z losowymi wyrazami i chcę z niego wyodrębnić tylko takie wyrazy, które składają się wyłącznie z dużych liter. Z tym nie mam problemu, wiem jak to zrobić za pomocą regexa, ale nie wiem, jak poruszać się po każdej linijce pliku i sprawdzać je z osobna.
Mój kod:
public class ZAD3 {
static String readFile(String pathname) throws IOException {
File file = new File(pathname);
StringBuilder fileContents = new StringBuilder((int) file.length());
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
fileContents.append(scanner.nextLine()).append(System.lineSeparator());
}
return fileContents.toString();
}
}
static String[] onlyUpperLettersStrings(String file) {
String regex = "[A-Z]+";
String[] correctWords = new String[(int) file.length()];
return correctWords;
}
public static void main(String[] args) throws IOException {
String file = new String("fileZAD3.txt");
// String[] correctWords = onlyUpperLettersStrings(file);
// System.out.println(file);
}
}
Czy w metodzie onlyUpperLetterStrings muszę używać bufferred readera? czy można to zrobić w zwykłej pętli? A może jakoś mogę uprościć kod z czytaniem pliku? Z góry dziękuję za pomoc :D