2015-06-25 97 views
0

請原諒我,我是Java新手。這是我目前的計劃的一部分。在這裏,我讀txt文件,並添加文件的某些行到一個ArrayList(工作,因爲它應該):獲取ArrayList中每個元素的特定字符的頻率<String>

public void actionPerformed(ActionEvent e) { 

    ArrayList<String> organismsString = new ArrayList<String>(); 
    boolean printLines = false; 
    StringBuilder organism = new StringBuilder(); 
    if (e.getSource() == openButton) { 
     returnVal = fileChooser.showOpenDialog(null); 
     if (returnVal == JFileChooser.APPROVE_OPTION) { 
      file = fileChooser.getSelectedFile(); 
      //File[] file = hairpinFileChooser.getSelectedFiles(); 
      //read file 
      try { 
       br = new BufferedReader(new FileReader(
        while ((currentLine = br.readLine()) != null) { 
         if (printLines) { 
          if (currentLine.startsWith(">")) { 
           // We have reached the next organism, so stop printing 
           printLines = false; 
           // Add the current organism to our collection 

           organismsString.add(organism.toString()); 


           // Clear the StringBuilder, ready for the next organism 
           organism.setLength(0); 

          } else { 
           // We are still printing the current organism 
           organism.append(currentLine); 

          } 
         } 

         if (currentLine.startsWith(organismId)) { 
    // Print this line, and start printing all lines after this (we don't want to append the current line) 
          //organism.append(currentLine); 
          printLines = true; 

         } 
        } 
        //Adds the final organism in the .txt file 
        organismsString.add(organism.toString()); 

但是我現在想算字母的頻率「G」和「 C「在arrayList的每個元素中。

目前我能夠統計ArrayList中存在的所有字母的頻率,但不是針對特定字母而是針對每個單獨元素。該代碼我要做到這一點如下:

char [] c = organism.toString().toCharArray(); 
          int sz = c.length; 
          int i = 0, j = 0, counter = 0; 

          for (i = 0; i < sz; i++) { 
           counter = 0; 
           for(j=0; j<sz; j++) { 
            if(j<i && c[i] == c[j]) { 
             break; 
            } 
            if (c[j] == c[i]) { 
             counter++; 
            } 
            if(j == sz-1) { 
            System.out.println("character " + c[i]+ " is present" +counter+" times"); 
            } 
           } 

          } 

如果任何人有我怎麼可能能夠進而達到這一點,將不勝感激任何幫助或建議!

希望這一切都是有道理的,但如果沒有請只問任何問題!

非常感謝:)

+0

你只是想只計算大寫字母 「G」 和/或 「C」?或者你還想包括小寫字母嗎? – Shar1er80

+0

不只是大寫的。我希望所有的資本G和C的總頻率對於數組列表的每個元素都一致,但是難以實現這一點,儘管我知道這可能是一個簡單的解決方案:) – Matt

回答

0

你可以有兩個int變量,一個是銫的量,一個用於GS的量。然後,依次循環訪問char數組中的元素。如果當前元素等於C,則增加C計數器。如果它等於G,則增加G計數器。

如果你只是想G和C的總數量,然後有一個計數器和增量,每次你遇到一個G或C.

0

我看到這兩個潛在的方法。

  1. 循環遍歷字符串中的每個字符,並在遇到'C'或'G'時遞增計數器。您不必將字符串轉換爲char []來遍歷字符,只需使用String.charAt()即可。
  2. 使用「[^ CG]」的正則表達式執行臨時String.replaceAll(),這意味着您要用空字符串替換所有不是'C'或'G'的字符。這將產生一個字符串,只有C和G,您可以撥打String.length()

示例代碼:

public static void main(String[] args) throws Exception { 
    String data = "GGGGGCABCKDJ930495860CCCGCGCGCCCGG"; 

    // Loop counting 
    int cgCount = 0; 
    for (int i = 0; i < data.length(); i++) { 
     if (data.charAt(i) == 'C' || data.charAt(i) == 'G') { 
      cgCount++; 
     } 
    } 

    System.out.printf("CG Count: %d\r\n", cgCount); 
    // String.replaceAll with regex pattern 
    System.out.printf("CG Count: %d\r\n", data.replaceAll("[^CG]", "").length()); 
} 

結果:

CG Count: 20 
CG Count: 20