Java illegal start of type error for return statement in string builder, why is this happening?

huangapple go评论75阅读模式
英文:

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 complementarySequence = new ArrayList<>();
static ArrayList templateStrand = new 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 &lt;String&gt; complementarySequence = new ArrayList &lt;&gt;();
static ArrayList &lt;Character&gt; templateStrand = new ArrayList &lt;&gt;();
public static void main(String [] args){
	System.out.println(&quot;Please enter the FASTA sequence.&quot;);
	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(&quot;complementary Sequence without commas equals &quot; + complementarySequence.join(&quot;&quot;));
	System.out.println(&quot;Your entered FASTA Sequence is &quot;+ templateStrand + &quot; your complimentary sequence is &quot; + System.out.println(arrayListToString(complementarySequence)));
}

static void compute_complimentary_sequence(Character nucleotide){
	
	Map &lt;Character, Character&gt; WatsonCrickPairs = new HashMap&lt;Character, Character&gt;();
	WatsonCrickPairs.put(&#39;A&#39;,&#39;T&#39;);
	WatsonCrickPairs.put(&#39;T&#39;,&#39;A&#39;);
	WatsonCrickPairs.put(&#39;G&#39;,&#39;C&#39;);
	WatsonCrickPairs.put(&#39;C&#39;,&#39;G&#39;);
	
	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&lt;String&gt; 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&lt;String&gt; list){
        StringBuilder sb = new StringBuilder();
        
        for(String nc2 : list){
            sb.append(nc2);
        }
       return sb.toString();
}

huangapple
  • 本文由 发表于 2020年9月23日 03:13:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64016334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定