2012-11-26 10 views
-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){ 

    } 




} 
+0

为什么我的问题打上了吗?我正在努力编程,并且对此很新颖。我也是一名生物化学家,所以这种计划正是我想要建立的东西。 –

回答

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 
    } 
} 
+0

非常有趣,虽然我的教练从来没有使用过的关键字! ARRRGH非常令人沮丧,因为我必须查找课堂上从未使用过的东西。 –

+0

if(UserInputDNA.matches(“。* A. *”)&& UserInputDNA.matches(“。* C. *”)&& UserInputDNA.matches(“。* T. *”)&& UserInputDNA.matches(“。* G 。“)){ \t \t \t checkStrand = true; \t \t} \t \t \t 其他\t { \t \t \t checkStrand = FALSE; \t \t \t System.err.println(“您没有输入有效的序列。”); \t \t} –

+0

^^^这也适用于我,但我一直打印出空 –

1

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

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

+0

嗯,我不明白,我的老师从来没有使用过特定的表达式“^ [ACTG] + $” 我使用eclipse的方式 –

+0

我喜欢String.matches()的想法,我在看JAVA API早些时候,找不到那么好的具体方法! –