0

行を文字列に保存する必要があります

元:

public function countOccurrences() {

    String Message = "Hello, how are you?"
    String trim = Mensaje.replaceAll(" ", "");


    char[] third = trim.toCharArray();

    for (int counter = 0; counter < third.length; counter++) {

        char ch = third[counter];
        int count = 0;

        for (int i = 0; i < third.length; i++) {

            if (ch == third[i]) {
                count++;
            }
        }


        boolean flag = false;
        for (int j = counter - 1; j >= 0; j--) {
            if (ch == third[j]) {
                flag = true;
            }
        }

        if (!flag) {

            trim = "Letra:" + ch + " se repite " + count + " veces ";
             // trim is a String where I need to store this information
        }


}

期待される結果:

  1. String: Message 内の各文字の出現回数をカウントする必要があります。
  2. この情報を txt ファイルに保存します

元:

次の情報を含む newfile.txt :

H, 2 times
e, 2 times
l, 2 times
o, 3 times
w, 1 times
a, 1 times
r, 1 times
e, 1 times
y, 1 times
u, 1 times

///

私はこれで誰かが投稿してみました:

トリム = トリム + "\n" + ch + ", " + カウント + " veces \n";

結果はファイル内で次のようになりました:(それは近いです)

こんにちは元気ですか?H、2回e、2回l、2回o、3回、、1回w、1回a、1回r、1回y、1回u、1回?、1回

4

3 に答える 3

0

グアバの方法:

Multiset<String> count = HashMultiset.create(Lists.charactersOf(message.replaceAll(" ", ""));
Files.write(Joiner.on("\n").join(Iterables.transform(count.entrySet(),
    new Function<Entry<String>, String>() {
        @Override public String apply(Entry<String> entry) {
            return String.format("%s, %d times", entry.getElement(), entry.getCount());
        }
    }, new File("whatever.txt"), Charsets.UTF_8);

か何か。

于 2012-07-03T02:17:43.450 に答える
0
public function countOccurrences() {

String Message = "Hello, how are you?"
String trim = Mensaje.replaceAll(" ", "");


char[] third = trim.toCharArray();

for (int counter = 0; counter < third.length; counter++) {

    if (ch == ',') {
        continue;
    }

    char ch = third[counter];
    int count = 0;

    for (int i = 0; i < third.length; i++) {

        if (ch == third[i]) {
            count++;
        }
    }


    boolean flag = false;
    for (int j = counter - 1; j >= 0; j--) {
        if (ch == third[j]) {
            flag = true;
        }
    }

    if (!flag) {

        trim = "Letra:" + ch + " se repite " + count + " veces ";
         // trim is a String where I need to store this information
    }

}

于 2012-07-03T02:21:23.060 に答える
0

問題はここから始まります:

    for (int i = 0; i < third.length; i++) {

        if (ch == third[i]) {
            count++;
        }
    }

私は2つの解決策を考えることができます。正しい方法は、文字からカウントへのマップを使用することです。これを行うと、 for ループ全体 (上記の抜粋部分だけでなく) を次の行に沿ったものに置き換えることができます。

Map letters = new HashMap<Character, Integer>();
for(char c : third) {
  Integer raw_count = letters.get(c);
  int count = raw_count == null ? 0 : raw_count.intValue();
  letters.put(c, count + 1);
}

次に、マップを反復処理して、その値を出力します。その部分が紛らわしい場合はコメントしてください。

マップについて聞いたことがない場合は、今がそれらについて学ぶ良い機会です。マップを使用できない場合(宿題...)、完全なコード サンプルを提供していないことは確かです。ただし、配列を使用して回避できます。26 要素の int 配列を作成します。次に、次のようなことができますletters[c - 'a']++。それを理解するために少し時間を取ってください。配列の最初の要素はas の数、2 番目の要素は s の数bなどです。最初は、これらはすべて 0 です。次に、見ている文字を取得します。 ( ) から(97 と 122 ではなく) と の間になるように減算し、そこの合計を増やしますc025

于 2012-07-03T02:07:08.513 に答える