2015-12-27 75 views
-3

我一直在嘗試解決這個問題一段時間,但無濟於事。當我運行代碼時,出現以下錯誤消息:不兼容的類型:edu.duke.StorageResource無法轉化爲行String geneList = FMG.storeAll(dna);上的java.lang.String。這是否意味着我試圖讓edu.duke對象使用java.lang.String類型的對象?我們將如何解決這個問題?StorageResource類型的錯誤消息

這裏是我到目前爲止的代碼:

package coursera_java_duke; 
import java.io.*; 
import edu.duke.FileResource; 
import edu.duke.StorageResource; 
import edu.duke.DirectoryResource; 

public class FindMultiGenes5 { 
    public int findStopIndex(String dna, int index) { 
     int stop1 = dna.indexOf("TGA", index); 
     if (stop1 == -1 || (stop1 - index) % 3 != 0) { 
      stop1 = dna.length(); 
     } 
     int stop2 = dna.indexOf("TAA", index); 
     if (stop2 == -1 || (stop2 - index) % 3 != 0) { 
      stop2 = dna.length(); 
     } 
     int stop3 = dna.indexOf("TAG", index); 
     if (stop3 == -1 || (stop3 - index) % 3 != 0) { 
      stop3 = dna.length(); 
     } 
     return Math.min(stop1, Math.min(stop2, stop3)); 
    } 

    public StorageResource storeAll(String dna) { 

     //CATGTAATAGATGAATGACTGATAGATATGCTTGTATGCTATGAAAATGTGAAATGACCCAdna = "CATGTAATAGATGAATGACTGATAGATATGCTTGTATGCTATGAAAATGTGAAATGACCCA"; 
     String geneAL = new String(); 
     String sequence = dna.toUpperCase(); 
     StorageResource store = new StorageResource(); 
     int index = 0; 


     while (true) { 
      index = sequence.indexOf("ATG", index); 
      if (index == -1) 
       break; 

      int stop = findStopIndex(sequence, index + 3); 

      if (stop != sequence.length()) { 
       String gene = dna.substring(index, stop + 3); 
       store.add(gene); 
       //index = sequence.substring(index, stop + 3).length(); 
      index = stop + 3; // start at the end of the stop codon 
      }else{ index = index + 3; 
     } 

    } 
    return store;//System.out.println(sequence); 
    } 
    public void testStorageFinder() { 
     DirectoryResource dr = new DirectoryResource(); 
     StorageResource dnaStore = new StorageResource(); 
    for (File f : dr.selectedFiles()) { 
     FileResource fr = new FileResource(f); 
     String s = fr.asString(); 
     dnaStore = storeAll(s); 
     printGenes(dnaStore); 
    } 


     System.out.println("size = " + dnaStore.size()); 

    } 
    public String readStrFromFile(){ 

     FileResource readFile = new FileResource(); 

     String DNA = readFile.asString(); 

     //System.out.println("DNA: " + DNA); 

     return DNA; 

    }//end readStrFromFile() method; 
    public float calCGRatio(String gene){ 

     gene = gene.toUpperCase(); 
     int len = gene.length(); 
     int CGCount = 0; 

     for(int i=0; i<len; i++){ 

      if(gene.charAt(i) == 'C' || gene.charAt(i) == 'G') 
       CGCount++; 

     }//end for loop 

     System.out.println("CGCount " + CGCount + " Length: " + len + " Ratio: " + (float)CGCount/len); 
     return (float)CGCount/len; 
    }//end of calCGRatio() method; 
    public void printGenes(StorageResource sr){ 

     //create a FindMultiGenesFile object FMG 
     FindMultiGenes5 FMG = new FindMultiGenes5(); 

     //read a DNA sequence from file 
     String dna = FMG.readStrFromFile(); 

     String geneList = FMG.storeAll(dna); 

     //store all genes into a document 
     StorageResource dnaStore = new StorageResource(); 

     System.out.println("\n There are " + geneList.size() + " genes. "); 

     int longerthan60 = 0; 
     int CGGreaterthan35 = 0; 
     for(int i=0; i<geneList.size(); i++){ 

      if(!dnaStore.contains(geneList.get(i))) 
       dnaStore.add(geneList.get(i)); 

      if(geneList.get(i).length() > 60) longerthan60++; 
      if(FMG.calCGRatio(geneList.get(i)) > 0.35) CGGreaterthan35++; 

     } 

     System.out.println("dnaStore.size: " + dnaStore.size()); 


     System.out.println("\n There are " + dnaStore.size() + " genes. "); 
     System.out.println("There are " + longerthan60 + " genes longer than 60."); 
     System.out.println("There are " + CGGreaterthan35 + " genes with CG ratio greater than 0.35."); 
    }//end main(); 
} 
+1

您沒有調用代碼的主要方法,所以我們甚至不知道正在運行的是什麼。我們也不知道輸入。 – Stultuske

+1

我對理解這件事有點困惑。 1:不確定edu軟件包是什麼,但沒有那些人不能測試你的代碼。 2:代碼看起來不會起作用。這兩個可能只是我,但是也沒有地方可以打印出你期望的輸出,fx「3:atgtaa」。 –

回答

0

我發現您的文章,因爲我在杜克使用這些edu.duke庫也做了類似的過程。

當我收到該錯誤消息時,這是因爲我使用了錯誤的方法來訪問它。

嘗試使用FMD.data()獲取所有基因字符串的迭代。