单词词频统计,为什么出不来效果?
分两个文件。一个是ReadTextFile.java。一个是ReadTextFileTest.java。后者包含main()。
第一个:
import java.io.File;
import
java.io.FileNotFoundException;
import
java.lang.IllegalStateException;
import
java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Collections;
public class ReadTextFile
{
private Scanner input;
public void openFile()
{
try
{
input = new Scanner (new File("clients.txt"));
}
catch(
FileNotFoundException file
NotFoundException)
{
System.err.println("Error openning file.");
System.exit(1);
}
}
public void readRecords()
{
String[] x = { ""};
List<String> list1 = Arrays.asList(x);
while (input.hasNext())
{
list1.add(input.next());
}
int frequency = Collections.frequency(list1, "culture");
System.out.printf(
"\nFrequency of red in list1: %d\n",frequency);
}// end try
}
-------------------------
第二个:
public class ReadTextFileTest
{
public static void main(String[] args)
{
ReadTextFile application = new ReadTextFile();
application.openFile();
application.readRecords();
}
}
-----------------
------解决方案--------------------参考一下:http://javapub.iteye.com/blog/1551604