[英]Finding the number of occurrences of certain characters within a JTextfield string
I'm pretty new to coding and I'm having trouble with my program.我对编码很陌生,我的程序有问题。 The program I'm trying to run takes the DNA strand that the user provides in a text field (can only include 'A', 'T', 'C', and 'G') and then the program gives you the complementary DNA strand.
我尝试运行的程序采用用户在文本字段中提供的 DNA 链(只能包括“A”、“T”、“C”和“G”),然后程序为您提供互补 DNA链。 That part is going well but I then need it to also output the number of times 'A', 'T', 'C', and 'G' occur in the original user's input (*not the complementary strand).
那部分进展顺利,但我还需要它 output 原始用户输入中出现“A”、“T”、“C”和“G”的次数(*不是互补链)。
I've found a lot of posts saying that I need to use the code that counts it (I have it in the last code block) but I haven't been able to get it to work with the program and I don't know what I would set the output of each JLabel to be.我发现很多帖子说我需要使用计算它的代码(我在最后一个代码块中有它)但我无法让它与程序一起工作而且我不知道我会将每个 JLabel 的 output 设置为什么。 Sorry this is so long, I just want to provide as much info as possible.
抱歉,这么长,我只想提供尽可能多的信息。
public void buttonPressed()
{
String str = input.getText().toUpperCase();
for (int i = 0; i <= str.length(); i++) {
char letter = str.charAt(i);
if (letter == 'A' || letter == 'T' || letter == 'C' || letter == 'G') {
str = input.getText().toUpperCase();
str = str.replace("A", "t")
.replace("T", "a")
.replace("C", "g")
.replace("G", "c")
.toUpperCase();
compOutput.setText(str);
}
else {
compOutput.setText("Error: input not in DNA format");
aOut.setText("0");
cOut.setText("0");
gOut.setText("0");
tOut.setText("0");
return;
}
}
}
/**
* This is where I'm having trouble, I can't tell if it's even counting
* the occurrences and if it is, I don't know how to display it in each JLabel.
*
* The JLabels are named aOut, tOut, cOut, gOut
*/
public int countingLetter(String str, char letter) {
int counter = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == letter) {
counter ++;
}
}
return counter;
}
}
Try the following code.试试下面的代码。 I used prints as I don't have JLabels available to print the results.
我使用打印,因为我没有 JLabel 可用于打印结果。 Note that if
countingLetter()
finds no letter, it will return a 0, so no need for an else
statement.请注意,如果
countingLetter()
没有找到字母,它将返回 0,因此不需要else
语句。
public void buttonPressed()
{
//String str = input.getText().toUpperCase();
String str = "ACTGTCA";
String originalUserInput = "ACTGTCA";
for (int i = 0; i <= str.length() - 1; i++) {
char letter = str.charAt(i);
if (letter == 'A' || letter == 'T' || letter == 'C' || letter == 'G') {
str = "ACTGTCA";
str = str.replace("A", "t")
.replace("T", "a")
.replace("C", "g")
.replace("G", "c")
.toUpperCase();
}
}
//compOutput.setText(str);
System.out.println("Complement: " + str);
System.out.println("originalUserInput: " + originalUserInput);
System.out.println("A: " + countingLetter(originalUserInput ,'A'));
System.out.println("T: " + countingLetter(originalUserInput ,'T'));
System.out.println("C: " + countingLetter(originalUserInput ,'C'));
System.out.println("G: " + countingLetter(originalUserInput ,'G'));
/*
aOut.setText(countingLetter(originalUserInput ,'A'));
tOut.setText(countingLetter(originalUserInput ,'T'));
cOut.setText(countingLetter(originalUserInput ,'C'));
gOut.setText(countingLetter(originalUserInput ,'G'));
*/
}
/**
* This is where I'm having trouble, I can't tell if it's even counting
* the occurrences and if it is, I don't know how to display it in each JLabel.
*
* The JLabels are named aOut, tOut, cOut, gOut
*/
public int countingLetter(String str, char letter) {
int counter = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == letter) {
counter ++;
}
}
return counter;
}
Let me know if it works.让我知道它是否有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.