Java代码不打印HashMap的键和值

时间:2016-11-30 21:42:04

标签: java hashmap subclass superclass

我的代码输入用户定义的字符串,并将其分类为DNA,RNA或ODD。输出应该打印HashMap nucleotide_hmap 的键和值,但代码当前没有。我应该在代码中更改哪些内容才能生效?

以下是代码应输出的内容: (如果用户输入序列ATGC)

  • ATGC
  • A 1:0.25
  • T 1:0.25
  • C 1:0.25
  • G 1:0.25
  • 序列长度:4
  • 逆序:CGTA
  • 反向补充:GCAT

这是我的一个子类:

package sequenceclasses; 

public class DNASequence extends MainSequence {
    public DNASequence(String dna_sequence){
        the_sequence = dna_sequence; 
        complement_hmap.put('A', 'T');
        complement_hmap.put('T', 'A');
        complement_hmap.put('C', 'G');
        complement_hmap.put('G', 'C');
    }   
    public void nuc_content(String dna_sequence){
        the_sequence = dna_sequence;
        double[] counts_ratios = new double[8];
        for (int i = 0; i < dna_sequence.length(); i++){
            if (dna_sequence.charAt(i) == 'A'){
                counts_ratios[0] += 1;}
            if (dna_sequence.charAt(i) == 'T'){
                counts_ratios[1] += 1;}
            if (dna_sequence.charAt(i) == 'C'){
                counts_ratios[2] += 1;}
            if (dna_sequence.charAt(i) == 'G'){
                counts_ratios[3] += 1;}
            }
            if (dna_sequence.length() > 0){
                counts_ratios[4] = counts_ratios[0] / dna_sequence.length();
                counts_ratios[5] = counts_ratios[1] / dna_sequence.length();
                counts_ratios[6] = counts_ratios[2] / dna_sequence.length();
                counts_ratios[7] = counts_ratios[3] / dna_sequence.length();
        }       
        String A_content = Double.toString(counts_ratios[0]) + " , " + Double.toString(counts_ratios[4]);
        String T_content = Double.toString(counts_ratios[1]) + " , " + Double.toString(counts_ratios[5]); 
        String C_content = Double.toString(counts_ratios[2]) + " , " + Double.toString(counts_ratios[6]);
        String G_content = Double.toString(counts_ratios[3]) + " , " + Double.toString(counts_ratios[7]);

        nucleotide_hmap.put('A', A_content);
        nucleotide_hmap.put('T', T_content);
        nucleotide_hmap.put('C', C_content);
        nucleotide_hmap.put('G', G_content);
    }
}

1 个答案:

答案 0 :(得分:0)

你永远不会在DNASequence类中调用nuc_content方法。要解决此问题,请将行nuc_content(dna_sequence);添加到DNASequence类。现在构造函数应该如下所示:

public DNASequence(String dna_sequence){
        the_sequence = dna_sequence; 
        complement_hmap.put('A', 'T');
        complement_hmap.put('T', 'A');
        complement_hmap.put('C', 'G');
        complement_hmap.put('G', 'C');
        nuc_content(dna_sequence);
    }