Also möchte ich, dass mein Programm die Anzahl der Absätze aus einer Textdatei zählt, aber leider beende ich 1 Nummer aus. Ich brauche die Antwort von 4, wenn ich 5. Hier werden immer ist der Text:Die Absätze werden falsch gezählt
Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in
Liberty, and dedicated to the proposition that all men are created equal.
Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so
dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a
portion of that field, as a final resting place for those who here gave their lives that that nation might
live. It is altogether fitting and proper that we should do this.
But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground.
The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add
or detract. The world will little note, nor long remember what we say here, but it can never forget
what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which
they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great
task remaining before us -- that from these honored dead we take increased devotion to that cause for which
they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have
died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of
the people, by the people, for the people, shall not perish from the earth.
Abraham Lincoln
November 19, 1863
Und hier ist mein Code:
public static void main(String[] args) {
String input;
Scanner kbd = new Scanner(System.in);
System.out.print("Enter the name of the input file: ");
input = kbd.nextLine();
try
{
// Set up connection to the input file
Scanner input1 = new Scanner(new FileReader(input));
// Set up connection to an output file
PrintWriter output=newPrintWriter(newFileOutputStream(input".txt"));
// initialize the counter for the line numbers
int lineNum = 0;
int words = 0;
int characters = 0;
int paragraphs = 0;
// as long as there are more lines left in the input file
// read from the input file, and copy to the output file
while (input1.hasNextLine())
{
// read a line from the input file
String line;
line = input1.nextLine();
// copy the line to the output file, adding a
// line number at the front of the line
output.println(line + "\n");
// increment the line counter
lineNum++;
//Section for counting the words
boolean word = false;
for (int i = 0; i < line.length(); i++) {
//checks for letters and counts as word till it finds a space then checks for a letter again.
if (!Character.isWhitespace(line.charAt(i)) && !word) {
words++;
word = true;
}
else if (Character.isWhitespace(line.charAt(i)) && word){
word = false;
}
}
characters += line.length();
paragraphs += getPara(line);
}
// close the files
input1.close();
output.close();
System.out.println("Lines: " + lineNum);
System.out.println("Words: " + words);
System.out.println("Characters: " + ((characters)));
System.out.println("Paragraphs: " + paragraphs);
}
catch(FileNotFoundException e)
{
System.out.println("There was an error opening one of the files.");
}
}
public static int getPara(String line){
int count = 0;
boolean p = false;
if (line.isEmpty() && !p){
count++;
p = true;
}
else if (!line.isEmpty() && p){
p = false;
}
return count;
}
}
ein Absatz ist eine Zeile gefolgt von einer Leerzeile. Die Methode liest nicht jede Zeile, sondern liest nur die erste Zeile – shmuellM