Java StorageResource类型的错误消息
我已经试着解决这个问题一段时间了,但是没有用。当我运行代码时,我收到以下错误消息:不兼容的类型:edu.duke.StorageResource无法转换为java.lang.String,位于Java StorageResource类型的错误消息,java,Java,我已经试着解决这个问题一段时间了,但是没有用。当我运行代码时,我收到以下错误消息:不兼容的类型:edu.duke.StorageResource无法转换为java.lang.String,位于String geneList=FMG.storeAll(dna)行。这是否意味着我试图使edu.duke对象与java.lang.String类型的对象一起工作?我们将如何解决这个问题 以下是我目前的代码: package coursera_java_duke; import java.io.*; imp
String geneList=FMG.storeAll(dna)行代码>。这是否意味着我试图使edu.duke对象与java.lang.String类型的对象一起工作?我们将如何解决这个问题
以下是我目前的代码:
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();
}
package coursera\u java\u duke;
导入java.io.*;
导入edu.duke.FileResource;
导入edu.duke.StorageResource;
导入edu.duke.DirectoryResource;
公共类FindMultiGenes5{
公共int findStopIndex(字符串dna,int索引){
int stop1=dna.indexOf(“TGA”,索引);
如果(stop1==-1 | |(stop1-索引)%3!=0){
stop1=dna.length();
}
int stop2=dna.indexOf(“TAA”,索引);
如果(stop2==-1 | |(stop2-索引)%3!=0){
stop2=dna.length();
}
int stop3=dna.indexOf(“标签”,索引);
如果(stop3==-1 | |(stop3-索引)%3!=0){
stop3=dna.length();
}
返回Math.min(stop1,Math.min(stop2,stop3));
}
公共存储资源storeAll(字符串dna){
//catgtaatagatgaatgaatgaatgatagatagcttgtatgctatgaaatgtgaatgaatgaccadna=“catgtaatagagatgaatgaatgaatgaatgaatgaatgaatgcttatgctatgaaatgaatgaatgacca”;
字符串geneAL=新字符串();
字符串序列=dna.toUpperCase();
StorageResource store=新的StorageResource();
int指数=0;
while(true){
索引=序列.索引of(“ATG”,索引);
如果(索引==-1)
打破
int stop=findStopIndex(序列,索引+3);
if(stop!=sequence.length()){
字符串基因=dna.子字符串(索引,stop+3);
储存。添加(基因);
//index=sequence.substring(index,stop+3).length();
index=stop+3;//从停止密码子的末尾开始
}else{index=index+3;
}
}
return store;//System.out.println(序列);
}
public void testStorageFinder(){
DirectoryResource dr=新的DirectoryResource();
StorageResource dnaStore=新的StorageResource();
对于(文件f:dr.selectedFiles()){
FileResource fr=新的FileResource(f);
字符串s=fr.asString();
dnaStore=存储所有数据;
printGenes(dnaStore);
}
System.out.println(“size=“+dnaStore.size());
}
公共字符串readStrFromFile(){
FileResource readFile=newfileresource();
字符串DNA=readFile.asString();
//System.out.println(“DNA:+DNA”);
返回DNA;
}//end readStrFromFile()方法;
公共浮动计算比率(字符串基因){
gene=gene.toUpperCase();
int len=基因长度();
int CGCount=0;
对于(inti=0;i0.35)CGGreaterthan35++;
}
System.out.println(“dnaStore.size:+dnaStore.size());
System.out.println(“\n有”+dnaStore.size()+“基因”);
println(“有“+longerthan60+”基因长于60。”);
系统输出println(“存在CG比率大于0.35的“+CGGreaterthan35+”基因”);
}//末端主管道();
}
我找到了你的帖子,因为我还在杜克大学使用那些edu.Duke库学习类似的课程
当我收到错误消息时,是因为我使用了错误的方法来访问它
尝试FMD.data()获取所有基因字符串的iterable。您没有调用代码的主方法,因此我们甚至不知道正在运行什么。我们也不知道输入。我在理解这一点时有点困惑。1:不确定edu包是什么,但是没有这些人就不能测试你的代码。2:代码看起来根本不起作用。这两个可能只是我,但也没有地方,你会打印输出,你似乎期待,fx“3:atgtaa”。