-1

我希望用户输入一个 DNA 序列,如果它没有字母 A、C、T 或 G,那么它应该打印出一个错误。但是如何扫描在构造方法 DNASequence 中输入的那些特定字符的字符串?

继承人我到目前为止。

import java.util.*;
public class DNASequence {

    private String DNASequence;//create a private static variable that can be accessed

    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        System.out.println("Please input a sequence of DNA: ");
        String DNAInput = input.nextLine();

    }


    public DNASequence(String DNAStrand){//Constructor Method that takes parameter a string and checks to see if its only A, T, C, G.

        DNASequence = DNAStrand;

        // Invoke the countLetters method to count each letter
        int[] counts = countLetters(DNAStrand.toUpperCase());

        // Display results
        for (int i = 0; i < counts.length; i++) {
          if (counts[i] != 0)
            System.out.println((char)('a' + i) + " appears  " +
              counts[i] + ((counts[i] == 1) ? " time" : " times"));
        }
      }

      /** Count each letter in the string */
      public static int[] countLetters(String s) {
        int[] counts = new int[26];

        for (int i = 0; i < s.length(); i++) {
          if (Character.isLetter(s.charAt(i)))
            counts[s.charAt(i) - 'a']++;
        }

        return counts;
      }



    public String toString(){//Method that just returns the stored sequence
        return DNASequence;

    }

    private static char NucleotideBaseCount(char BaseCount){//Method to count bases

    }

    private static boolean isSubsequenceOf(String DNAStrand){

    }




}
4

2 回答 2

1

您可以为此使用以下正则表达式^[ACTG]+$.

要将输入字符串与正则表达式匹配,请使用String.matches().

于 2012-11-26T20:11:52.847 回答
0

这里是基于 #NPE 评论的示例实现:

import java.util.*;

public class DNASequence
{
  private String DNASequence = null; //create a private static variable that can be accessed

  public static void main(String[] args)
  {
    System.out.println("Please input a sequence of DNA: ");
    DNASequence dnaS = new DNASequence((new Scanner(System.in)).nextLine().toUpperCase());
  }

  //Constructor Method that takes parameter a string and checks to see if its only A, T, C, G.
  public DNASequence(String DNAStrand) throws IllegalArgumentException
  {
    if (DNAStrand.matches("^[ATCG]+$"))
    {
      DNASequence = DNAStrand;
    }
    else
    {
      throw new IllegalArgumentException("DNA Sequences should only contain A, T, C, G charaters");
    }

  }

  /** Count each letter in the string */
  public int[] countLetters() throws IllegalArgumentException
  {
    int[] counts = new int[4];

    if (DNASequence != null)
    {
      for (int i = 0; i < DNASequence.length(); i++)
      {
        switch (DNASequence.charAt(i))
        {
          case 'A':
            counts[0]++;
            break;
          case 'T':
            counts[1]++;
            break;
          case 'C':
            counts[2]++;
            break;
          case 'G':
            counts[3]++;
            break;
          default:
            throw new IllegalArgumentException("DNA Sequences should only contain A, T, C, G charaters, found: " + DNASequence.charAt(i));
        }
      }
    }

    return counts;
  }

  //Method that just returns the stored sequence
  public String toString()
  {
    return DNASequence;
  }

  private char NucleotideBaseCount(char BaseCount){//Method to count bases
    return 'a'; // replace with real implementation
  }

  private boolean isSubsequenceOf(String DNAStrand)
  {
    return false; // repalce with real implementation
  }
}
于 2012-11-26T20:43:16.623 回答