这是我正在努力的一项编程任务。我目前正在努力使用代码的主要方法。据说,我想做的是使用 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;
}
}
}