2 Replies - 1133 Views - Last Post: 24 March 2012 - 05:49 PM Rate Topic: -----

#1 maximusblacksdad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-March 12

Find greatest common DNA sequence (null return from method!?)

Posted 24 March 2012 - 11:04 AM

So the objective is to find the greatest common sequence in two samples of text now the problem i'm having is i'm getting a return of null about 20 times from my method and I don't know why any help is appreciated thanks!

import java.util.*;
import java.io.*;

public class DNA
{
  public static String[] greatComSeq(String small, String big)
  {
    int x = big.length();
    int y = small.length();
    int arrayNum = 0;
    String u = "";
    String subSmall = "";
    String[] sequences = new String[x+1];

    for (int k = 0; k < subSmall.length(); k++)
    {
      for (int i = k; i < subSmall.length(); i++)
      {
        subSmall = small.substring(k, i+1);
        if (big.indexOf(subSmall) >= 0)
        {
          sequences[arrayNum] = subSmall;
          arrayNum++;
        }
      }
    }
    return sequences;
  }
  public static void main (String[] args) throws IOException
  {
    File inFile = new File ("./dna.txt");

    Scanner input = new Scanner(inFile);
    String number = input.nextLine();
    int num = Integer.parseInt(number);
    String smallString = "";
    String bigString = "";
    int var1 = 0;
    int var2 = 0;
    int x = 0;
    int y = 0;
    int z = 1;
    String[] seqList = new String[10];


    for (int i = 0; i < num; i++)
    {
      String lineOne = input.nextLine();
      String lineTwo = input.nextLine();
      String first = lineOne.toUpperCase();
      String second = lineTwo.toUpperCase();

      if (first.length() <= second.length())
      {
        smallString = first;
        bigString = second;
      }
      else
      {
        smallString = second;
        bigString = first;
      }
      seqList = greatComSeq(smallString, bigString);
      for (int j = 0; j < seqList.length; j++)
      {
        System.out.println(seqList[j]);
      }
    }
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Find greatest common DNA sequence (null return from method!?)

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Find greatest common DNA sequence (null return from method!?)

Posted 24 March 2012 - 01:56 PM

This creates an array of String[] of length x+1
String[] sequences = new String[x+1];

this prints all the String in the array

for (int j = 0; j < seqList.length; j++)
System.out.println(seqList[j]);

but nothing garanties that you will have String in every slot of the array as you fill it only on certain condition

if (big.indexOf(subSmall) >= 0)
sequences[arrayNum] = subSmall;

May be yoou can do something like

     for (int j = 0; j < seqList.length; j++) {
        if(seqList[j] == null)
           break;
        System.out.println(seqList[j]);
     }


Was This Post Helpful? 0
  • +
  • -

#3 codeMonkey_1066  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 71
  • Joined: 22-March 12

Re: Find greatest common DNA sequence (null return from method!?)

Posted 24 March 2012 - 05:49 PM

You may be better off using a List instead of an array.
as in
 // String[] sequences = new String[x+1];
 List<String> sequences = new ArrayList<String>();
 
 ...
 if (big.indexOf(subSmall) >= 0)
 {
  sequences.add(subSmall);
 }



If you still need to return an array rather than a List then you can use the toArray() method of List.

In most cases your better off using a List or other class from the collections framework than an array.
The only main exception is when performance ( memory or speed ) is a big issue.

Also for iterating over the resulting list it's better to use a for loop of the form
 for(String seq : sequences )
 {
  System.out.println(seq);
 }


This works if sequences is a List or an array of strings.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1