0

这是我正在努力的一项编程任务。我目前正在努力使用代码的主要方法。据说,我想做的是使用 isDNASequence 方法检查字符串中的各个字符,看看它是否在要求范围内,同时还计算输入之间的空间量(例如,如果 args 输入是aaaa gggg cccc tttt acgt accg atgc gcta tccg <= 应该有 9 个空格)从中创建一个二维数组。我计划首先使用 for 循环来使用 isDNASequence 方法,但现在由于某种原因 charAt 方法不起作用,我被困在这部分代码中。因此,非常感谢任何指示或帮助。我是一个非常大的java菜鸟,所以如果我问任何愚蠢的问题,请原谅我:D

import java.util.ArrayList;
import java.util.List;

public class Hamming {

public static boolean isDNASequence(String s) {
    // create an array of acceptable characters
    char[] acceptableInput = { 'a', 'c', 'g', 't' };
    // render the original string s to lower case into string x
    String x = s.toLowerCase();

    // for loop to check if all the characters are acceptable characters or
    // not
    for (int i = 0; i < x.length(); i++) {
        boolean isDNASequence = false;
        for (int j = 0; j < acceptableInput.length; j++) {
            if (x.charAt(i) == acceptableInput[j])
                isDNASequence = true;
        }
        // the output of the boolean
        if (isDNASequence == false)
            return false;
    }
    return true;

}

public static int[][] getDistances(String[] sequences) {
    // filter out the data and get the number of strings left
    List<String> validStrings = new ArrayList<>();
    for (int x = 0; x < sequences.length; x++) {
        if (isDNASequence(sequences[x])) {
            validStrings.add(sequences[x]);
        }
    }

    // create the 2D array
    int size = validStrings.size();
    int[][] result = new int[size][];
    for (int i = 0; i < size; i++) {
        result[i] = new int[size];
    }

    // putting the values from getHammingDistance method into the array cell
    for (int z = 0; z < result.length; z++) {
        for (int j = 0; j < result[0].length; j++) {
            result[z][j] = getHammingDistance(sequences[z], sequences[j]);

        }

    }

    return result;

}

public static int getHammingDistance(String sequence1, String sequence2) {
    // Initialise int a, which will be the Hamming distance
    int a = 0;
    /**
     * renders the two strings sequence1 and sequence2 into lower case to
     * make it easier to calculate
     */
    String sequenceX = sequence1.toLowerCase();
    String sequenceY = sequence2.toLowerCase();

    /**
     * I use the for-loop to calculate the Hamming distance based on
     * comparing individual characters of the two strings
     */
    for (int x = 0; x < sequenceX.length(); x++) {
        if (sequenceX.charAt(x) != sequenceY.charAt(x)) {
            a += 1;
        }
    }
    return a;
}

/**
 * Main method
 * 
 * 1. Go through the parameters 2. Ensure they are valid DNA sequences 3.
 * Get the Hamming distance matrix 4. Print out the highest distance only
 * 
 * @param args
 *            - program arguments
 */
public static void main(String[] args) {
    int d = 0;
    for (int q = 0; q < args.length; q++) {
        isDNASequence(args.charAt[q]);
    } if (args.charAt[q] == " "){
        d+=1;

    }

}

}

4

2 回答 2

0

charAt是一个方法,而 args 是一个 String[] 所以应该使用它args[0].charAt(q)

我个人会做这样的事情。

String arr = args[0].split(" "); //I wouldnt bother with spaces

然后我会做以下事情;

for(int i = 0; i < arr.length; i++){
    if(arr[i].length() != 4) //I m assuming in every 4 char there should be space 
         return false;
    for(int j = 0; j < arr[i].length();i++)
         return isDNAsequence(arr[i].charAt(j));
}

isDNASequence(String str)改成isDNASequence(char c)

于 2013-04-28T19:12:51.607 回答
0

in main 方法是字符串数组,args它们不是字符数组。
所以,主要的方法应该是:

public static void main(String[] args) {
    int d = 0;
    for (int q = 0; q < args.length; q++) {
        isDNASequence(args[q]);
    }
    if (args[q].equals(" ")){
        d+=1;
    }
}

如果您希望代码args[q].equals(" ")检测参数中的空格,我认为您不需要它,因为如果您从命令提示符运行代码,空格将被视为参数的分隔符。
例如,您在命令提示符下运行此类:

> java Hamming aaaa gggg cccc tttt acgt accg atgc gcta tccg

然后,argsmain 方法中的值将是{"aaaa","gggg","cccc","tttt","acgt","accg","atgc","gcta","tccg"}

于 2013-04-28T19:00:22.897 回答