我正在尝试使用BufferedReader构建程序,该程序读取文件并跟踪元音,单词,并且可以计算每行的平均字数。我有骨架来阅读文件,但我真的不知道从哪里拿它。任何帮助,将不胜感激。感谢。
import java.io.*;
public class JavaReader
{
public static void main(String[] args) throws IOException
{
String line;
BufferedReader in;
in = new BufferedReader(new FileReader("message.txt"));
line = in.readLine();
while(line != null)
{
System.out.println(line);
line = in.readLine();
}
}
}
答案 0 :(得分:1)
您可以使用扫描仪传递该行并检索该字符串行的每个标记。
line = line.replaceAll("[^a-zA-Z]", ""); //remove all punctuation
line = line.toLowerCase(); //make line lower case
Scanner scan = new Scanner(line);
String word = scan.next();
然后你可以遍历每个标记来计算每个单词中的元音。
for(int i = 0; i < word.legnth(); i++){
//get char
char c = word.charAt(i);
//check if the char is a vowel here
if("aeiou".indexOf(c) > -1){
//c is vowel
}
}
你需要做的就是设置一些反向跟踪来跟踪这些并且你在笑。
啊,如果你想确保没有诸如“ - ”之类的非单词计算为单词,最简单的方法可能是从文本中删除所有非字母数字字符。 我也在上面添加了它。
line = line.replaceAll("[^a-zA-Z]", "");
line = line.toLowerCase();
哦,因为你是java新手,不要忘记导入
import java.util.Scanner;
答案 1 :(得分:1)
这就是我得到的。计数这个词是值得怀疑的,但是我会给出一个例子。可以做出改变(我接受批评)。
import java.io.*;
public class JavaReader
{
public static void main(String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new FileReader("message.txt"));
String line = in.readLine();
// for keeping track of the file content
StringBuffer fileText = new StringBuffer();
while(line != null) {
fileText.append(line + "\n");
line = in.readLine();
}
// put file content to a string, display it for a test
String fileContent = fileText.toString();
System.out.println(fileContent + "--------------------------------");
int vowelCount = 0, lineCount = 0;
// for every char in the file
for (char ch : fileContent.toCharArray())
{
// if this char is a vowel
if ("aeiou".indexOf(ch) > -1) {
vowelCount++;
}
// if this char is a new line
if (ch == '\n') {
lineCount++;
}
}
double wordCount = checkWordCount(fileContent);
double avgWordCountPerLine = wordCount / lineCount;
System.out.println("Vowel count: " + vowelCount);
System.out.println("Line count: " + lineCount);
System.out.println("Word count: " + wordCount);
System.out.print("Average word count per line: "+avgWordCountPerLine);
}
public static int checkWordCount(String fileContent) {
// split words by puncutation and whitespace
String words[] = fileContent.split("[\\n .,;:&?]"); // array of words
String punctutations = ".,:;";
boolean isPunctuation = false;
int wordCount = 0;
// for every word in the word array
for (String word : words) {
// only check if it's a word if the word isn't whitespace
if (!word.trim().isEmpty()) {
// for every punctuation
for (char punctuation : punctutations.toCharArray()) {
// if the trimmed word is just a punctuation
if (word.trim().equals(String.valueOf(punctuation)))
{
isPunctuation = true;
}
}
// only add one to wordCount if the word wasn't punctuation
if (!isPunctuation) {
wordCount++;
}
}
}
return wordCount;
}
}
示例输入/输出:
文件:
This is a test. How do you do?
This is still a test.Let's go,,count.
输出:
This is a test. How do you do?
This is still a test.Let's go,,count.
--------------------------------
Vowel count: 18
Line count: 4
Word count: 16
Average word count per line: 4.0