简体   繁体   中英

How to print letters(DNA) in a format of (0,10,6)

I am struggling with this code here. I want print out dna in java that shows format of (0, 10, 6) which need to pass a until test instead of looking like this

ACAAGATGCC ATTGTCCCCC GGCCTCCTGC TGCTGCTGCT CTCCGGGGCC ACGGCCACCG 
CTGCCCTGCC CCTGGAGGGT GGCCCCACCG GCCGAGACAG CGAGCATATG CAGGAAGCGG 
CAGGAATAAG GAAAAGCAGC CTCCTGACTT TCCTCGCTTG GTGGTTTGAG TGGACCTCCC 
AGGCCAGTGC CGGGCCCCTC ATAGGAGAGG AAGCTCGGGA GGTGGCCAGG CGGCAGGAAG 
GCGCACCCCC CCAGCAATCC GCGCGCCGGG ACAGAATGCC CTGCAGGAAC TTCTTCTGGA 
AGACCTTCTC CTCCTGCAAA TAAAACCTCA CCCATGAATG CTCACGCAAG TTTAATTACA 

It looks like this ATTGTCCCCCGGCCTCCTGCTGCTGCTGCTCTCCGGGGCCACGGCCACCGCTGCCCTGCCCCTGGAGGGTGGCCCCACCGGCCGAGACAGCGAGCATATGCAGGAAGCGGCAGGAATAAGGAAAAGCAGCCTCCTGACTTTCCTCGCTTGGTGGTTTGAGTGGACCTCCCAGGCCAGTGCCGGG....

here is my code

public String formatInGroups(int index, int basesPerGroup, int groupsPerLine) {
      StringBuilder formattedSequence = new StringBuilder();
      String sequence = sequences.get(index);
      int num = 0;
       
      while(num < sequence.length()) {
         for(int i = 0; i < groupsPerLine; i++) {
            for( int j = 0; j < basesPerGroup; j++) {
               if(num < sequence.length()) {
                  formattedSequence.append(sequence.charAt(num));
                  num++;
               }
            }
                       
               
      
      }
     
        
   }
  return sequence;
}
}

You should append a white space to the sequence after a dna sequence is appended (at the end of the inner for loop). Also, when a line is full, you should append a new line (\n) character to the sequence (at the end of the outer for loop).

public String formatInGroups(int index, int basesPerGroup, int
groupsPerLine) {
  StringBuilder formattedSequence = new StringBuilder();
  String sequence = sequences.get(index);
  int num = 0;
   
  while(num < sequence.length()) {
     for(int i = 0; i < groupsPerLine; i++) {
        for( int j = 0; j < basesPerGroup; j++) {
           if(num < sequence.length()) {
              formattedSequence.append(sequence.charAt(num));
              formattedSequence.append(" ");
              num++;
           }
        }
        formattedSequence.append("\n");
                   
           
  
  }
 
    
}
return formattedSequence.toString();
}
}

Looking at your code, I think you just missed some little things here and there, add a new line break after a certain character count and a space after some groups, and also you were returning the wrong variable. Here check this code I edited based on yours, I just added some simple stuff and you got the remaining right.

public String formatInGroups(int index, int basesPerGroup, 
 int groupsPerLine) {
        // I suppose you have a 'sequences' list somewhere
        sequences.add(dna());
        StringBuilder formattedSequence = new StringBuilder();
        String sequence = sequences.get(index);
        int num = 0;

        while(num < sequence.length()) {
            for( int j = 0; j < basesPerGroup; j++) {
                if(num < sequence.length()) {
                    if (num % (basesPerGroup * groupsPerLine) == 0){
                        formattedSequence.append("\n");
                    }
                    formattedSequence.append(sequence.charAt(num));
                    num++;
                }
            }
            formattedSequence.append(" ");
        }
    return formattedSequence.toString().trim();
}

Little things to consider on your next problem:

1 - You were returning your original sequence instead of the formattedSequence.toString() ;

2 - Try to avoid using global variables, you can declare them inside your for loop;

3 - Try using better variable names, instead of num you could name your variable after something that it is doing, like charPositionCounter , it will improve your code readability.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM