Es fällt mir schwer das erste Wort von jeder Zeile zu bekommen. Irgendwelche Ideen, warum es leere Dateien ausgibt?Das erste Wort von jeder Zeile erhalten?
Das Programm führt den folgenden Text als Eingabe:
abecedism word created from the initials of words in a phrase
ablaut variation in root vowel of words to change meaning
acronym word formed from initial letters of another word
acrophonic using a symbol for the initial sound of a thing
acroteleutic phrase or words at the end of a psalm
adversative word or phrase expressing opposition
Und das Ziel ist es, die Ergebnisse zu erhalten, wie folgt aussehen:
abecedism
ablaut
acronym
acrophonic
acroteleutic
adversative
Hier ist der Code so weit:
public static void main(String args[]) {
String fileNameOutput = "OutputFile.txt";
String fileName = "InputWords.txt";
Charset cs = Charset.defaultCharset();
try (BufferedReader bReader = Files.newBufferedReader(Paths.get(fileName), cs)) {
PrintWriter outputStream = new PrintWriter(fileNameOutput);
int lineNum = 0;
String line = null;
while ((line = bReader.readLine()) != null) {
lineNum++;
if (line.split(" ").length > 1) continue;
outputStream.println(line);
}
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Was passiert, wenn mehr als ein Leerzeichen zwischen diesen beiden Wörtern aus der ersten Zeile "abecedism word" steht? Deine 'line.split (" ")' wird hier nicht funktionieren. – bili