2

ArrayList に含まれる単語の数を計算しようとしています。すべての単語が別の行にある場合にこれを行う方法を知っていますが、次のように一部の単語が同じ行にある場合:

hello there
blah
cats dogs

したがって、すべてのエントリを調べて、現在のエントリに含まれる単語の数をどうにかして調べる必要があると考えています。たとえば、次のようになります。

    public int numberOfWords(){
    for(int i = 0; i < arraylist.size(); i++) {
        int words = 0;
        words = words + (number of words on current line);
    //words should eventually equal to 5
    }
return words;
}

私は正しいと思いますか?

4

3 に答える 3

5

ループのすべての反復中に再割り当てされないint wordsループの外側で宣言してインスタンス化する必要があります。intこの構文を使用しfor..eachてリストをループすることができます。これにより、リストからget()アイテムを取り出す必要がなくなります。split1 行の複数の単語を に処理しStringArray内の項目を数えますArray

public int numberOfWords(){
    int words = 0;
    for(String s:arraylist) {         
      words += s.split(" ").length;
    }
    return words;
}

総テスト

public class StackTest {

    public static void main(String[] args) {
        List<String> arraylist = new ArrayList<String>();
        arraylist.add("hello there");
        arraylist.add("blah");
        arraylist.add("   cats    dogs");
        arraylist.add(" ");
        arraylist.add(" ");
        arraylist.add(" ");

        int words = 0;
        for(String s:arraylist) {
            s = s.trim().replaceAll(" +", " "); //clean up the String
            if(!s.isEmpty()){ //do not count empty strings
               words += s.split(" ").length;
            }
        }
        System.out.println(words);
    }
}
于 2013-04-25T09:11:59.913 に答える
1

次のようになります。

public int numberOfWords(){
    int words = 0;
    for(int i = 0; i < arraylist.size(); i++) {
        words = words + (number of words on current line);
    //words should eventually equal to 5
    }
return words;
}
于 2013-04-25T09:12:30.287 に答える
0

私はこれがあなたを助けることができると思います.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
public class LineWord {

public static void main(String args[]) {
    try {
        File           f        = new File("C:\\Users\\MissingNumber\\Documents\\NetBeansProjects\\Puzzlecode\\src\\com\\test\\test.txt");    // Creating the File passing path to the constructor..!!
        BufferedReader br       = new BufferedReader(new FileReader(f));    //
        String         strLine  = " ";
        String         filedata = "";

        while ((strLine = br.readLine()) != null) {
            filedata += strLine + " ";
        }

        StringTokenizer stk   = new StringTokenizer(filedata);
        List <String>  token = new ArrayList <String>();

        while (stk.hasMoreTokens()) {
            token.add(stk.nextToken());
        }

        //Collections.sort(token);
        System.out.println(token.size());
        br.close();
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
}
}

したがって、この場合、ファイルからデータを赤くし、それらをトークン化した後にそれらをリストに格納します。それらを数えるだけです。コンソールから入力を取得したいだけの場合は、Bufferedreader を使用し、それらをトークン化し、スペースで区切り、リストに入れます。 、簡単に取れるサイズ。

あなたが探しているものを手に入れたことを願っています。

于 2013-04-25T09:49:52.193 に答える