0

I need to write a Java program for a course I'm taking which looks for genes in a strand of DNA.The Issue I am having is that from the test method, I need to pass printAllgenes(a) to the void printAllgenes method. In the test method I've tried setting 'int a' to 'String a', but in either case an error when compiling explaining that void cannot be converted to int or String. I'm sure its obvious, but I'm very new to programming, so please pardon my ignorance! Thank you.

import java.io.*;
import edu.duke.*;

public class FindProtein {

  public void test() {
    String a = "atg aaa tab tag atg aaa tga aat ag";
    int b = printAllgenes(a);
    System.out.println("DNA string is " + a);
    System.out.println("Gene found is " + b);
  }

  public void printAllgenes(String dna) {
    int sp = 0; //start point
    while (true) {
      int start = dna.indexOf("atg,sp");
      if (start == -1) {
        break;
      }
      int stop = findStopIndex(dna, start + 3);
      if (stop != dna.length()) {
        System.out.println(dna.substring(start, stop + 3));
        sp = stop + 3;
      } else {
        sp = sp + 3;
      }
    }
  }

  public int findStopIndex(String dna, int index) {
    int tga = dna.indexOf("tga", index);
    if (tga == -1 || (tga - index) % 3 != 0) {
      tga = dna.length();
    }
    int taa = dna.indexOf("taa", index);
    if (taa == -1 || (taa - index) % 3 != 0) {
      taa = dna.length();
    }
    int tag = dna.indexOf("tag", index);
    if (tag == -1 || (tga - index) % 3 != 0) {
      tag = dna.length();
    }
    return Math.min(tga, Math.min(taa, tag));
  }
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Ryan
  • 35
  • 1
  • 4

2 Answers2

1

Try to use just:

 printAllgenes(a); 

Because printAllgenes method doesn't have any type of return statement.

Abdelhak
  • 8,299
  • 4
  • 22
  • 36
  • ah ok. That worked. Thank you! – Ryan Feb 25 '16 at 10:32
  • Sorry, one more thing- How would I then print the results in the test method from the printAllgenes since it isn't assigned to a variable? – Ryan Feb 25 '16 at 10:34
  • You can use like this: public int printAllgenes(String dna){ //some code... return sp; @Ryan – Abdelhak Feb 25 '16 at 10:36
  • you need to reimplement `printAllgenes()` Method. just add a return type `int` to it and return what variable u want to return from inside of Method... – Vikrant Kashyap Feb 25 '16 at 10:36
  • It can only remove the compilation error not been able to achieve what he exactly wants to achieve ... – Vikrant Kashyap Feb 25 '16 at 10:42
  • please dont't be in hurry please always try to know what he actually want to achieve. I am not her for arguing sorry but I have commented only not downvoted you .. i am not a dominator :) – Vikrant Kashyap Feb 25 '16 at 10:47
  • Actually, in the end, this was the solution . Regardless, thank you both for your help – Ryan Feb 25 '16 at 12:17
1

change return type void to int It will return your count whatever u want to return from printAllgenes(String dns) Method. You will get a int return which will initialize you variable b that is being displayed on Console.

public int printAllgenes(String dna){
      int sp = 0; //start point
          while (true){
          int start = dna.indexOf("atg,sp");
             if (start==-1){
                break;
                }   
              int stop = findStopIndex(dna,start+3);
              if (stop!=dna.length()){
                   System.out.println(dna.substring(start,stop+3));
                   sp=stop+3;
                   }
             else{
                 sp=sp+3;
             }        
        } 
        return sp;

    }

Now Your Test Method Implementation will work fine...

public void test(){
String a= "atg aaa tab tag atg aaa tga aat ag";
int b = printAllgenes(a);     
System.out.println("DNA string is " +a);
System.out.println("Gene found is "+b);    
 }

Thank you..

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52