检查文件是否包含字符串并为新字符串创建数组

时间:2014-03-09 05:06:45

标签: java arrays file

我需要创建一个读取文件的方法,并检查文件中的每个单词。文件中的每个新单词都应存储在字符串数组中。该方法应该不区分大小写。请帮忙。

该文件说明如下: 不要问你的国家能为你做些什么 问你能为国家做些什么

所以数组应该只包含:询问,不是,你的,国家,能做什么,做什么,你

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();
            }
        }
    }
}

}

2 个答案:

答案 0 :(得分:0)

你总是可以尝试:

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的所有行比单独读取每行更清晰。你的方法的问题是你计算的行数,而不是单词的数量。如果一行中有多个单词,则输出结果不正确。

或者,如果您不使用Java 7,则可以按如下方式创建行列表,然后计算最后的单词(与countWords()中的方法相反:

List<String> allLines = new ArrayList<String>();

Scanner fileScanner = new Scanner(yourFile);
while (fileScanner.hasNextLine()) {
     allLines.add(scanner.nextLine());    
}

fileScanner.close();

然后按照前面的代码分割每一行并创建数组。另请注意,您应该在扫描仪周围使用try{} catch块,而不是理想的throws

答案 1 :(得分:0)

您可以利用我的以下代码段(它不会存储重复的字词)!

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
your