问题
For example, if I have the following lines of text in a file:
this is an example. this is an example.
this is an example. this is an example. this is an example
this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an example.
I want to be able to count these lines as 3 paragraphs. Now my code will count this as 4 paragraphs, as it does not know when a paragraph begins and ends.
Scanner file = new Scanner(new FileInputStream("../.../output.txt"));
int count = 0;
while (file.hasNextLine()) { //whilst scanner has more lines
Scanner s = new Scanner(file.nextLine());
if(!file.hasNext()){
break;
}
else{
file.nextLine();
count++;
}
s.close();
}
System.out.println("Number of paragraphs: "+ count);
file.close();
This is what I have so far. It reads lines of text, and treats each line as a single paragraph.
I want it to treat lines of text that don't have any empty line between them as 1 paragraph and count all paragraphs in file.
回答1:
You will not be able to see the spaces or newline characters using Scanner. nextLine()
method eliminates the \n's.
You need to use a class and methods that reads the bytes of the file so you can detect the spaces and newline characters.
Try to use read()
method of FileInputStream
.
回答2:
Scanner probably isn't the best choice if you only want to count lines. BufferedReader is probably better.
BufferedReader in = new BufferedReader(new FileReader("output.txt"));
String line = in.readLine();
int count = 0;
StringBuilder paragraph = new StringBuilder();
while (true) {
if (line==null || line.trim().length() == 0) {
count++;
System.out.println("paragraph " + count + ":" + paragraph.toString());
paragraph.setLength(0);
if(line == null)
break;
} else {
paragraph.append(" ");
paragraph.append(line);
}
line = in.readLine();
}
in.close();
System.out.println("Number of paragaphs: "+ count);
来源:https://stackoverflow.com/questions/22155836/java-how-to-use-scanner-to-read-and-count-paragraph