Reputation: 55
Alright So This is an Assignment, so Dont help me too much... i just need to return a DnaSequence the object. and thats it. Uhm heres the constructor
import java.io.File;
import java.lang.String;
public class DnaSequence {
public char[] dna;
public DnaSequence(char[] dna) {
String str = String.valueOf(dna);
str.replaceAll("[^ATCG]", "");
dna = str.toCharArray();
for (int i = 0; i < dna.length; i++) {
this.dna[i] = dna[i];
}
return dna;
}
}
I will be using io.file and lang.string later.
And here is the Main (for testing )
public class DnaSequencetEST {
public static void main(String[] args) {
char[] bases = { 'G', 'A', 'T', 'T', 'A', 'C', 'A' };
DnaSequence seq = new DnaSequence(bases);
System.out.println(seq);//Should print "GATTACA"
}
}
And below is a link to what my finish result should do and or look like.(just for fun if you wanna look at it) http://jeff.cis.cabrillo.edu/datasets/12j_dnasequence/DnaSequence.html
So DnaSequence.java:14 error: incompatible types: unexpected return value return dna: ^ So yea Thats what ive been staring at for like 5 hours, Any help would be much appreciated
Upvotes: 0
Views: 137
Reputation: 165
I don't know all the requirements but to pass that test you just need to implement the toString()
on DnaSequence
and return the DNA sequence.
Note the System.out.println
will always call the toString
if the object passed is an Object
.
Solution:
public class DnaSequence {
public final String dna;
public DnaSequence(char[] dna) {
this.dna = String.valueOf(dna).replaceAll("[^ATCG]", "");
}
@Override
public String toString() {
return this.dna;
}
}
Upvotes: 0
Reputation: 201447
A constructor is for initializing object internal state. Here that would logically be the char[] dna
field (and don't make fields public
just "because"). Next, a String
is immutable; so str.replaceAll("[^ATCG]", "");
without an assignment is pointless (but you can chain it before calling to toCharArray()
). You need to override toString()
for the behavior you want. And you can simply pass a char[]
to the String
constructor. Like,
public class DnaSequence {
private char[] dna;
public DnaSequence(char[] dna) {
this.dna = new String(dna).replaceAll("[^ATCG]", "").toCharArray();
}
@Override
public String toString() {
return new String(dna);
}
}
Upvotes: 2