2012-11-26 12 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早些時候,找不到那麼好的具體方法! –