如何仅扫描四个特定字符的字符串?

时间:2012-11-26 20:10:02

标签: java

我希望用户输入DNA序列,如果它没有字母A,C,T或G,那么它应该打印出错误。但是如何扫描为constructot方法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){

    }




}

2 个答案:

答案 0 :(得分:1)

您可以使用以下regular expression^[ACTG]+$

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

答案 1 :(得分: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
  }
}