Search code examples
javagenetics

How to pass a String parameter to a void method?


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

Solution

  • Try to use just:

     printAllgenes(a); 
    

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