我停留在一段代码上,该代码将从文本文件中读取输入,然后计算某个单词出现的次数。我已经获得了4个班级TextReader
。 TextReader
类读取输入文件,名为readNextWord
的方法将返回输入文件中的下一个单词。 WordCount
,WordCollecter
(我被困的那个)和DisplayWords
。
对于WordCollector
类,我有以下代码:
public class WordCollector {
TextReader reader;
ArrayList countWord;
DisplayWords display;
public WordCollector(String word)
{
TextReader reader = new TextReader(word);
ArrayList<WordCount> countWord = new ArrayList<WordCount>();
DisplayWords display = new DisplayWords();
}
我需要编写一个私有方法来读取TextReader
中的单词,构造WordCounts
并将它们存储在ArrayList
中,并保留读入的单词总数的计数。WordCollector
的构造函数应该调用私有方法,确保完全读入输入文件,并在构造WordCollector
时完成所有频率计数。我似乎无法理解这一部分。
这是一个例子,但这只使用一个类:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Collections;
public class reader
{
public void main(String name)
{
FileReader fr = null;
BufferedReader br =null;
String [] stringArray;
int counLine = 0;
int arrayLength ;
String s="";
String stringLine="";
try{
fr = new FileReader(name);
br = new BufferedReader(fr);
while((s = br.readLine()) != null){
stringLine = stringLine + s;
stringLine = stringLine + " ";/*Add space*/
counLine ++;
}
stringArray = stringLine.split(" ");
arrayLength = stringArray.length;
System.out.println("There are "+arrayLength + " words");
/*Duplicate String count code */
for (int i = 0; i < arrayLength; i++) {
int c = 1 ;
for (int j = i+1; j < arrayLength; j++) {
if(stringArray[i].equalsIgnoreCase(stringArray[j])){
c++;
for (int j2 = j; j2 < arrayLength; j2++) {
stringArray[j2] = stringArray[j2+1];
arrayLength = arrayLength - 1;
}
}//End of If block
}//End of Inner for block
System.out.println(stringArray[i]+" appears "+c+" times .");
}//End of Outer for block
System.out.println("The number of Lines is "+counLine);
System.out.println();
fr.close();
br.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:2)
您尚未明确定义自己的问题。对我而言,这看起来像是一项任务。因为你是新人,我会提供一些指导。
您必须使用OOP(面向对象编程)。
我提供了完整的工作代码。使用此指南并根据您的要求进行修改。
字数
package tutorial;
public class WordCount {
private String word;
private int count;
public WordCount(String word, int count) {
super();
this.word = word;
this.count = count;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
Word Collector
package tutorial;
import java.io.File;
import java.io.IOException;
public class WordCollector {
private TextReader textReader;
public WordCollector(File file) throws IOException {
textReader = new TextReader(file);
}
public WordCount getWordCounts(String inputWord) {
String[] extractWords = textReader.extractWords();
int totalCount = 0;
for (String word : extractWords) {
if (inputWord.equals(word)) {
totalCount++;
}
}
return new WordCount(inputWord, totalCount);
}
}
TextReader(这使用Apache FileUtil库来读取文件)
FileUtil Docs 包教程;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class TextReader {
private String fileString;
public TextReader(File file) throws IOException {
fileString = FileUtils.readFileToString(file);
}
public String[] extractWords() {
return fileString.split(" ");
}
}
<强> DisplayWord 强>
package tutorial;
import java.io.File;
import java.io.IOException;
public class DisplayWord {
private WordCollector wordCollector;
public DisplayWord(File inputFile) throws IOException {
wordCollector = new WordCollector(inputFile);
}
public void displayCountWord(String word) {
WordCount wordCounts = wordCollector.getWordCounts(word);
System.out.println("The word " + word + " has appeared "
+ wordCounts.getCount() + " times");
}
}
快速测试
package test;
import java.io.File;
import java.io.IOException;
import org.junit.Test;
import tutorial.DisplayWord;
public class TestWordCount {
@Test
public void test() throws IOException {
DisplayWord displayWord = new DisplayWord(new File("D://test.txt"));
displayWord.displayCountWord("Church");
}
}
示例文本文件
Bond is a bad boy.Bond goes to temple EveryDay.Then bond everday goes to Church Church is a good place for people
lab lab lab
<强>输出强>
The word Church has appeared 2 times