java - 以 10 个字符为一组的序列

标签 java eclipse applet sequence stringbuffer

我正在完成这项作业,我想知道如何一次以 10 个字符为一组显示序列。

下面是工作程序截图: enter image description here

我想在输出框中对10个字符进行分组,例如:

1 CTCTAACGCG CAAGCGCATA TCCTTCTAGG
61 ....

每行大约有 60 个字符,不包括空格和数字,因此必须有 6 组,每组 10 个字符。

下面是我为显示此输出而编写的代码:

public void dispLines() {
    // Get the selected value of characters per line and assign it to noc variable
    String noc = numOfChar.getSelectedItem().toString();
    // Call StringBuffer object and assign sb variable to it
    StringBuffer sb = new StringBuffer();
    // Assign raw dna data to dna variable, where string will be mutated
    String dna = rawDNAInput.getText();
    // Create newdna variable to store the newly created data
    String newdna = "";
    // Loop through the size of raw dna
    for (int i = 0 ; i < dna.length (); ++i)
    {
        // Assign every single character to StringBuffer sb
        sb.append(dna.charAt (i));              
    }       
    // Assign the StringBuffer sb values to the newdna variable
    newdna = sb.toString();
    // Recall StringBuffer object, so new data can be assigned
    sb = new StringBuffer();
    // Assign start varaible of 0
    int start = 0;
    // Assign end varaible to be start + number of characters per line
    int end = start + Integer.parseInt(noc);
    // Keep looping till end value is less than the length of the dna
    while(end < newdna.length())
    {
        // Append values into StringBuffer sb varaible by calling makeNumberedStr method
        sb.append(makeNumberedStr(newdna.substring(start, end), start + 1));
        // Increment start variable by the selected numbers of characters per line
        start += Integer.parseInt(noc);
        // Increment end variable by the selected numbers of characters per line
        end += Integer.parseInt(noc);
    }
    // Append values into StringBuffer sb varaible by calling makeNumberedStr method
    sb.append (makeNumberedStr (newdna.substring (start), start + 1));
    String result = sb.toString();
    for(int i = 0; i < result.length(); i++) {

    }
    // Check to make sure uppercase is selected, if it is then make every character uppercase, else make them lowercase
    if(upperCase.isSelected()) {
        DNAOutput.setText(result.toUpperCase());
    } else if(lowerCase.isSelected()) {
        DNAOutput.setText(result.toLowerCase());
    }  
}

/*
 * makeNumberedStr
 * This method only displays required number of characters per line
 * @parameters String x and integer num
 * @returns new StringBuffer value
 */
private String makeNumberedStr (String s, int num)
{
    // makes and returns a string composed from left to right of:
    //   a 6 character field containing right justified [num] followed by 2 spaces
    //   the string s followed by \n
    // Call new StringBuffer object and give it a length of raw dna + 8
    StringBuffer sb = new StringBuffer (s.length ());
    // Create nstr String varaible and give it value of num
    String nstr = String.valueOf (num);
    // Loop through the nstr length and append blank space
    for (int i = 0 ; i < 6 - nstr.length () ; ++i)
        sb.append (' ');
    // Check if display number is selected, or else do not display number on every line
    if(indexNum.isSelected() == true)
        sb.append (nstr + "  ");
    // Append s value to String Buffer
    sb.append (s);
    // Append new line to StringBuffer
    sb.append ('\n');        
    // Return StringBuffer text
    return sb.toString();
}

谢谢您,非常感谢!

最佳答案

运行这个程序,这样你就有了一个长字符串“s”,之后我只需添加代码(自动计算字符的数量,当计算到十时,它会自动在,,之间添加空格),这将帮助您在每十个字符后添加空格,即使您不需要计算它们...

public class PracticeOne {

public static void main(String [] args)
{
    String s = "aaaaaaaaaaaaaaaaaaaaaaaaa";
    System.out.println(s.replaceAll(".{10}", "$0 "));

}
     }

结果是

啊啊啊啊啊啊啊啊啊啊啊

希望这对你有帮助

关于java - 以 10 个字符为一组的序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15846595/

相关文章:

java - 如何让弹跳球移动得更快?动态速度?

java - 在 JBOSS 7.1.1 中排除 EAR 中的模块

java - @SpringBootApplication - ComponentScan 在新的 Eclipse 项目中无法按预期工作

java - 小程序能做什么和不能做什么

java - 从 applet 发送到多个文件到 servlet

java - 将 "On Change"监听器放在 jFormattedTextField 上

java - 需要从实体管理器获得不止一个结果

java - eclipse中检查样式的代码审查

java - eclipse 上下文菜单引用层次结构,这是什么意思?

java - 如何在最新版本的 Eclipse IDE 中用 Java 创建小程序?