2012-07-03 48 views
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 :消息
  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 +」,「+ count +」veces \ n「;

,結果是該文件中:(它更接近)

您好,Howareyou? H,2次e,2次l,2次,3次,1次w,1次 a,1次r,1次y,1次u,1次?,1次

+0

要在@jonnyGold擴大:你能解釋一下什麼結果你想要的,和/或得到期望的結果,爲這個特殊的輸入? –

+0

也許翻譯輸出也將有所幫助...這是一個國際性的網站 – renatoargh

+0

輸出說「信:H重複1次」,「信:O重複3次」等 –

回答

0

問題從這裏開始:

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

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

我能想到兩個解決方案。正確的方法是使用從字符到計數的Map。如果你這樣做,你可以沿着線的東西取代你的整個循環(不僅僅是上面摘錄的部分):

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']++。花一點時間來消化 - 我們說陣列中的第一個元素是a s的計數,第二個是b s的數量等等。最初這些都是0.然後我們把我們的角色查看(c),從中減去,使其介於025(非97和122)之間,並在那裏增加總數。

Si necesitas ayuda enespañol,pues dime。 Nacíen洪都拉斯。

0

Guava方式:

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

什麼的。

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 
    } 

}

+0

我有個問題,如果我需要爲每個單詞創建一個計數器,我該怎麼辦?例如:if(third [i] ==「A」){counterA ++} – m4g4bu

+0

我的答案中的代碼沒有工作嗎?我認爲櫃檯沒有問題。 – Sabbath