2015-10-13 108 views
0

我有一個任務的程序,我無法解開最後的消息。它需要能夠接受大量「DNA」代碼片段。給出的樣品在100,000+範圍內。我首先寫了一個小樣本,一行很奇妙的工作。 電訊局長告訴我,我應該可以添加一個while (input.hasNext()),它將完成比我複製並粘貼到控制檯中的示例文件的第一行更多的內容。當然是這樣!它只是不會結束。我試着用我認爲合適的break;,但最終回到我所在的位置,只有一條線被計算在內。雖然hasNext()不會結束

Scanner scan = new Scanner(System.in); //Scanner 

System.out.println("Enter a DNA sequence consisting of A, T, G, and C, on one line: "); //Instructions for user. 
dnaSequence = scan.nextLine();               //Scan for next line of string. 
dnaSequence = dnaSequence.toUpperCase();            //Converts all letters entered upper case to avoid issues. 

while(scan.hasNext()){ 
    for (int i = 0; i < dnaSequence.length(); i++) //Make i = 0, i has to be less than the length of the entered sequence, will excute count. 
    { 
     validCount = dnaSequence.charAt(i); //[FILL IN!] 
     switch (validCount)     //Switch for all valid counts 
     { 
     case 'A' : //For any case A. 
      countA++; //Count all As. 
      break; 
     case 'T' : //For any case T. 
      countT++; //Count all Ts. 
      break; 
     case 'C' : //For any case C. 
      countC++; //Count all Cs. 
      break; 
     case 'G' : //For any case G. 
      countG++; //Count all Gs. 
      break; 
     } 
    } 

    totalCountGC = countG + countC;       //Math for G and C, together. 
    totalCountSequence = countA + countT + countG + countC; //Math for total count of all other counts in switch. 

回答

3

您從不使用循環內的任何輸入。你居然讀出新的數據的唯一情況是你進入while循環之前,在這條線:

dnaSequence = scan.nextLine(); 

所以基本上你正在做的是從閱讀你的輸入單行線,然後繼續做你的計算在同一條線上反覆。

移動和toUpperCase裏面的循環,它會不斷讀取新的行,並最終消耗所有的輸入。 所以您的代碼會是這個樣子:

while(scan.hasNextLine()){ 
    String dnaSequence = scan.nextLine().toUpperCase(); 

    for (int i = 0; i < dnaSequence.length(); i++){ 
     validCount = dnaSequence.charAt(i); 
     switch (validCount){ 
     case 'A' : 
      countA++; 
      break; 
     case 'T' : 
      countT++; 
      break; 
     case 'C' : 
      countC++; 
      break; 
     case 'G' : 
      countG++; 
      break; 
     } 
    } 
} 

在此,我假設你正在使用像輸入重定向從文件爲您的輸入,並在手動行不打字。如果你確實在運行時鍵入了它們,那麼這將不起作用,因爲程序無法知道你什麼時候完成。

+3

人們也將認爲它會更適合使用'hasNextLine'的,而不是'hasNext'了'而-loop' – MadProgrammer

+0

我知道這是什麼愚蠢簡單!非常感謝你,我一直在討論java文檔和我的教科書,試圖找出我出錯的地方。非常感激。另外我不知道我可以結合nextline()和upperCase()。 – GoldenDesign

0

System.in是一個輸入流通常

  • 從控制檯
  • 讀取數據,並且被打開的應用程序正在工作整個時間。

現在,因爲很可能用戶在創建將被髮送到數據流的中間hasNext需要與決定要等到(他可以在控制檯寫它,但沒有按回車鍵還)

  • 用戶將決定派他的數據,
  • 或直到流將(在文件結束的情況下等)

爲了避免這個問題,你可以讓被關閉用戶提供某種小號這將結束環路像

while(scanner.hasNextLine()){ 
    String line = scanner.nextLine(); 
    if ("EXIT".equals(line)) 
     break; 
    //if we are here it means loop should continue 
    handle(line);//or other code which will handle your data 
} 

pecial值,以避免在打開的流無限等待(如System.in),您可以選擇其他選項。簡單地讓掃描器從源文件中讀取,其中已結束,如文件。

所以,你的代碼可以是這樣的:

//input.txt should contain data you want to read 
File data = new File("path/to/your/input.txt"); 
Scanner scanner = new Scanner(data); 
while(scanner.hasNextLine()){ 
    String line = scanner.nextLine(); 
    //handle this line 
}