我的程序是获取限制为正则表达式[gcatGCAT]的字符串,并将该字符串转换为互补字符字符串。互补字符是g到c,c到g,a到你,t到a。
例如,用户输入的字符串"gact"应生成"cuga"。
但是,当输入大约50 个字符或以上的字符串时(我没有计算我输入了多少个字符,只是按了键盘上的 g 和 c 键一会儿。 我想我用完了很多计算机的 RAM,因为程序冻结了,我的操作系统背景发生了变化或其他什么。(我不想尝试复制它,因为我很害怕)
我是否使用了太多的 ram 来插补这么大的字符串并对其执行操作?如果这是问题所在,有什么方法可以简化它以不使用那么多内存?我现实地希望能够接受 200 到 500 个字符的东西,其中 1000 个字符是最理想的。我是编码新手,所以如果我错了,请原谅我。
import java.text.DecimalFormat;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import java.util.Random;
//getting my feet wet, 1/13/2015, program is to take a strand of nucleotides, G C A T, for DNA and give
//the complementary RNA strand, C G U A.
public class practiceSixty {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
String input = null;
boolean loopControl = true;
char nucleotide;
int repeat;
do{
do{
input = JOptionPane.showInputDialog(null, " Enter the sequence of nucleotides(G,C,A and T) for DNA, no spaces ");
}while(!input.matches("[GCATgcat]+")); //uses regular expresions, This method returns true if, and only if, this string matches the given regular expression.
JOptionPane.showMessageDialog(null, "the data you entered is " + input);
StringBuilder dna = new StringBuilder(input);
for(int i = 0; i < input.length(); i++)
{
nucleotide = input.charAt(i);
if(nucleotide == 'G' || nucleotide == 'g' )
{
dna.setCharAt(i, 'c');
}
else if( nucleotide == 'C' || nucleotide == 'c')
{
dna.setCharAt(i, 'g');
}
if(nucleotide == 'A' || nucleotide == 'a')
{
dna.setCharAt(i, 'u');
}
else if(nucleotide == 'T' || nucleotide == 't')
{
dna.setCharAt(i, 'a');
}
}
JOptionPane.showMessageDialog(null, "the DNA is , " + input + " the RNA is " + dna);
repeat = JOptionPane.showConfirmDialog(null, "Press Yes to continue, No to quit ", "please confirm", JOptionPane.YES_NO_OPTION);
}while(repeat == JOptionPane.YES_OPTION);
}
});
}
}
只使用string.replace()。 但是,您不能只用另一个替换一个,因为这会搞砸以后的替换。
input = input.toLowerCase();
input = input.replace('a','1');
input = input.replace('c','2');
input = input.replace('g','3');
input = input.replace('t','4');
input = input.replace('1','u');
input = input.replace('2','g');
input = input.replace('3','c');
input = input.replace('4','a');