英文:
Java illegal start of type error for return statement in string builder, why is this happening?
问题
public class Homework_two {
static Scanner keyb = new Scanner(System.in);
static ArrayList
static ArrayList
public static void main(String[] args) {
System.out.println("Please enter the FASTA sequence.");
String FASTA_Sequence = keyb.nextLine();
for (char ch : FASTA_Sequence.toCharArray()) {
templateStrand.add(ch);
compute_complementary_sequence(ch);
}
System.out.println("Your entered FASTA Sequence is " + templateStrand +
" your complimentary sequence is " + arrayListToString(complementarySequence));
}
static void compute_complementary_sequence(Character nucleotide) {
Map<Character, Character> WatsonCrickPairs = new HashMap<>();
WatsonCrickPairs.put('A', 'T');
WatsonCrickPairs.put('T', 'A');
WatsonCrickPairs.put('G', 'C');
WatsonCrickPairs.put('C', 'G');
for (Map.Entry<Character, Character> nc : WatsonCrickPairs.entrySet()) {
Character key = nc.getKey();
Character value = nc.getValue();
if (nucleotide == key) {
complementarySequence.add(Character.toString(value));
}
}
}
static String arrayListToString(ArrayList<String> list) {
StringBuilder sb = new StringBuilder();
for (String nc2 : list) {
sb.append(nc2);
}
return sb.toString();
}
}
英文:
I am attempting to output the results of a complementary dna sequence but i am needing to show output as a string so ive designed a string builder to give me my output as a string but I am receiving an illegal start of type error for the return statement. Why am I receiving this error? I've looked over everything and nothing seems to stick out to me.
public class Homework_two{
static Scanner keyb = new Scanner(System.in);
static ArrayList <String> complementarySequence = new ArrayList <>();
static ArrayList <Character> templateStrand = new ArrayList <>();
public static void main(String [] args){
System.out.println("Please enter the FASTA sequence.");
String FASTA_Sequence = keyb.nextLine();
//templateStrand.add(FASTA_Sequence);
//compute_complimentary_sequence(templateStrand);
for(char ch: FASTA_Sequence.toCharArray()){
templateStrand.add(ch);
compute_complimentary_sequence(ch);
}
//System.out.println("complementary Sequence without commas equals " + complementarySequence.join(""));
System.out.println("Your entered FASTA Sequence is "+ templateStrand + " your complimentary sequence is " + System.out.println(arrayListToString(complementarySequence)));
}
static void compute_complimentary_sequence(Character nucleotide){
Map <Character, Character> WatsonCrickPairs = new HashMap<Character, Character>();
WatsonCrickPairs.put('A','T');
WatsonCrickPairs.put('T','A');
WatsonCrickPairs.put('G','C');
WatsonCrickPairs.put('C','G');
for(Map.Entry nc : WatsonCrickPairs.entrySet()){
Character key = (Character)nc.getKey();
Character value = (Character)nc.getValue();
if(nucleotide == key){
complementarySequence.add(Character.toString(value));
}
}
}
static String arrayListToString(ArrayList<String> list){
StringBuilder sb = new StringBuilder();
for(String nc2 : list){
sb.append(nc2);
}
} return sb.toString();
}
答案1
得分: 1
因为你的返回语句应该位于另一个部分。
static String arrayListToString(ArrayList<String> list){
StringBuilder sb = new StringBuilder();
for(String nc2 : list){
sb.append(nc2);
}
return sb.toString();
}
英文:
Because your return statement should locate in another section.
static String arrayListToString(ArrayList<String> list){
StringBuilder sb = new StringBuilder();
for(String nc2 : list){
sb.append(nc2);
}
return sb.toString();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论