java—查找jtextfield字符串中某些字符的出现次数

bn31dyow  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(354)

我对编码相当陌生,我的程序有问题。我尝试运行的程序获取用户在文本字段中提供的dna链(只能包括“a”、“t”、“c”和“g”),然后程序将提供互补的dna链。这部分进行得很顺利,但我需要它同时输出“a”、“t”、“c”和“g”在原始用户输入中出现的次数(*不是互补串)。

我发现很多帖子说我需要使用计算它的代码(我在最后一个代码块中有它),但我无法让它与程序一起工作,我不知道我会将每个jlabel的输出设置为什么。抱歉,这太长了,我只想提供尽可能多的信息。

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;
    }       
}
nlejzf6q

nlejzf6q1#

请尝试以下代码。我使用了prints,因为我没有可用的jlabel来打印结果。注意,如果 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;
}

让我知道它是否有效。

相关问题