Double to string Java Loop

时光毁灭记忆、已成空白 提交于 2019-12-12 06:08:51

问题


Hey so I am trying to get the GC-content of a DNA string is given by the percentage of symbols in the string that are C or G. For example, the GC-content of "AGCTATAG" is .375 or 37.5%. Here is what I came with up. I am having trouble with the calculations and returning the double as string.

public static double gcContent(String dna) {
    //TODO Implement this method
      double gcContent = 0;
      double count=0; 
      for (int i = 0; i < dna.length(); i ++) {
          if (gcContent == dna.length()){
              gcContent = (dna.length()/ 2) ;
          }
          return double.toString (gcContent); 
      }
  }

回答1:


Your calculation doesnt make sense. You have to iterate over each char of your dna-string and compare this with your expected value char ('C' or 'G', upper and lower case?) If you want to return the result as string, you have to change the return type to String, too.

public static String gcContent(String dna) {
    //TODO Implement this method
    char c = 'C';
    char g = 'G';
      double gcContent = 0;
      double count=0; 
      for (int i = 0; i < dna.length(); i ++) {

          if (dna.charAt(i) == c || dna.charAt(i) == g){
              count++;
          }
      }
      gcContent = count / (double)dna.length();
      return String.valueOf(gcContent); 
  }



回答2:


You cant call toString() in primitive type variable. You can use :

String.valueOf(gcContent)



回答3:


There are many ways to do this.

String.valueOf and Double.toString will work but gives you no control over the format.

Using a number formatter would be much more powerful as it lets you control the way the output is presented. So you can make it a decimal fraction, or a percentage.

Some reading for you:

https://docs.oracle.com/javase/tutorial/java/data/numberformat.html http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html




回答4:


I think you can use Double.toString(gcContent);




回答5:


This might not be best one, but I use this,

return gcContent + "";

How does this work can be see here : Java: How to concatenate Double to String and here : How does the String class override the + operator?

Also, you need to change your function's return type to String.




回答6:


If you insist to use toString you can box the value in a Double object like this:

new Double(gcContent).toString();

Else the most apropriate way I think is to use String.format, because you can format the string. For example if you want two digits after the decimal point you have:

return String.format("%.2f", gcContent);



回答7:


You need to count the C and G characters. Then you can use String.format(String, Object...) to return a formatted String. Something like,

public static String gcContent(String dna) {
    if (dna == null || dna.isEmpty()) return "0%";
    int count = 0;
    for (char ch : dna.toUpperCase().toCharArray()) {
        switch (ch) {
        case 'G': case 'C':
            count++;
        }
    }
    return String.format("%.1f%%", 100 * (count / (double) dna.length()));
}

public static void main(String[] args) {
    System.out.println(gcContent("AGCTATAG"));
}

Output is (as requested)

37.5%


来源:https://stackoverflow.com/questions/33163789/double-to-string-java-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!