我需要创建一个方法来读取文件,并检查文件中的每个单词。文件中的每个新词都应该存储在字符串数组中。该方法应该不区分大小写。请帮帮忙。
该文件写道:不要问你的国家能为你做什么,你要问你能为你的国家做什么
所以数组应该只包含: ask,not,what,your,country,can,do,for,you
import java.util.*;
import java.io.*;
public class TextAnalysis {
public static void main (String [] args) throws IOException {
File in01 = new File("a5_testfiles/in01.txt");
Scanner fileScanner = new Scanner(in01);
System.out.println("TEXT FILE STATISTICS");
System.out.println("--------------------");
System.out.println("Length of the longest word: " + longestWord(fileScanner));
System.out.println("Number of words in file wordlist: " );
countWords();
System.out.println("Word-frequency statistics");
}
public static String longestWord (Scanner s) {
String longest = "";
while (s.hasNext()) {
String word = s.next();
if (word.length() > longest.length()) {
longest = word;
}
}
return (longest.length() + " " + "(\"" + longest + "\")");
}
public static void countWords () throws IOException {
File in01 = new File("a5_testfiles/in01.txt");
Scanner fileScanner = new Scanner(in01);
int count = 0;
while(fileScanner.hasNext()) {
String word = fileScanner.next();
count++;
}
System.out.println("Number of words in file: " + count);
}
public static int wordList (int words) {
File in01 = new File("a5_testfiles/in01.txt");
Scanner fileScanner = new Scanner(in01);
int size = words;
String [] list = new String[size];
for (int i = 0; i <= size; i++) {
while(fileScanner.hasNext()){
if(!list[].contains(fileScanner.next())){
list[i] = fileScanner.next();
}
}
}
}}
发布于 2014-03-09 13:24:40
你可以试着:
List<String> words = new ArrayList<String>();
//read lines in your file all at once
List<String> allLines = Files.readAllLines(yourFile, Charset.forName("UTF-8"));
for(int i = 0; i < allLines.size(); i++) {
//change each line from your file to an array of words using "split(" ")".
//Then add all those words to the list "words"
words.addAll(Arrays.asList(allLines.get(i).split(" ")));
}
//convert the list of words to an array.
String[] arr = words.toArray(new String[words.size()]);使用Files.readAllLines(yourFile, Charset.forName("UTF-8"));读取yourFile的所有行要比单独读取每一行要干净得多。你的方法的问题是你计算的是行数,而不是字数。如果一行中有多个单词,则输出将不正确。
或者,如果您不使用Java7,您可以创建一个行列表,如下所示,然后计算末尾的单词数(与countWords()中的方法相反
List<String> allLines = new ArrayList<String>();
Scanner fileScanner = new Scanner(yourFile);
while (fileScanner.hasNextLine()) {
allLines.add(scanner.nextLine());
}
fileScanner.close();然后,如前面的代码所示,拆分每一行并创建数组。还要注意,理想情况下,您应该在扫描仪周围使用try{} catch块,而不是throws。
发布于 2014-03-09 13:42:56
你可以利用我下面的代码片段(它不会存储重复的单词)!
File file = new File("names.txt");
FileReader fr = new FileReader(file);
StringBuilder sb = new StringBuilder();
char[] c = new char[256];
while(fr.read(c) > 0){
sb.append(c);
}
String[] ss = sb.toString().toLowerCase().trim().split(" ");
TreeSet<String> ts = new TreeSet<String>();
for(String s : ss)
ts.add(s);
for(String s : ts){
System.out.println(s);
}输出结果为:
ask
can
country
do
for
not
what
you
yourhttps://stackoverflow.com/questions/22278647
复制相似问题