javakook
javakook

Reputation: 113

Double to string Java Loop

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); 
      }
  }

Upvotes: 0

Views: 532

Answers (7)

Elliott Frisch
Elliott Frisch

Reputation: 201477

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%

Upvotes: 0

Winusch
Winusch

Reputation: 38

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); 
  }

Upvotes: 2

George
George

Reputation: 7573

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);

Upvotes: 0

Ghazanfar
Ghazanfar

Reputation: 1469

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.

Upvotes: 0

user2063688
user2063688

Reputation: 36

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

Upvotes: 0

Sammy
Sammy

Reputation: 477

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

Upvotes: 0

Rahman
Rahman

Reputation: 3795

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

String.valueOf(gcContent)

Upvotes: 1

Related Questions