2011-12-13 62 views
2

因此,首先,這是家庭作業,儘管不是我的。這是我的姐夫的。自從我做電腦以來,他問我尋求幫助,但我只用C++工作。他正在使用System.in將文本輸入到文件中,直到他獲得EOF。之後,他創建了一個實例Scanner並調用實例上的nextLine以嘗試獲取filename並獲得NoSuchElementException。根據Javadoc的說法,這意味着沒有任何輸入信息會被接收到,在使用System.in並在鍵盤上打字時看起來像是一件奇怪的事情。我的懷疑是EOF字符不知道怎麼消耗。他的代碼在今晚的午夜時間到期,他還完成了一切(我建議他使用虛擬文件名並回到問題)。獲取NoSuchElementException使用Scanner類讀取Standard.In讀取EOF後

這裏是他的代碼:

import java.util.*; 
import java.io.*; 

public class FileTest 
{ 
    public static void main(String[] args) 
{ 
     createFile(); 
     readFile(); 
    } 

public static void createFile() 
{ 

    //Variables 
    InputStream istream; 
    PrintStream ostream; 
    istream = System.in; 
    ostream = System.out; 
    Scanner keyboard = new Scanner(System.in); 
    int lastEntry = 0; 
    final int EOF = -1; 


    //Asks user for filename. 
    try 
    { 
     String fileName; 
     System.out.println("Please enter the filename of the file you want to open: "); 
     fileName = keyboard.next(); 

     //Creates specified file.   
     File currentFile = new File(fileName); 

     //Checks if file already exists. 
     while(currentFile.exists()) 
     { 
      System.out.println(fileName + " already exists"); 
      System.out.println("Error: To prevent tis file from being overwritten please enter another file name"); 
      fileName = keyboard.nextLine();   
     } 

     //Asks user for information they want stored in file.   
     try 
     { 
      ostream = new PrintStream(fileName); 
      System.out.println("Please enter what you would like to put in the file and press Ctrl+Z when finished: "); 

      //Writes information to file. 
      try 
      { 
       while((lastEntry = istream.read()) != EOF) 
       ostream.write(lastEntry); 

      } 
      catch(Exception e) 
      { 
       System.out.println("Error: " +e.getMessage()); 
      } 

     }   
     catch(Exception f) 
     { 
      System.out.println("Error: " +e.getMessage()); 
     } 
    } 

    finally 
    { 
    } 

} 

public static void readFile() 
{ 
     InputStream input; 
     PrintStream output; 
     output = System.out; 
     int lastEntry = 0; 
     final int EOF = -1; 
     Scanner keyboard2 = new Scanner(System.in); 

     //Asks user for filename. 
     String newFile; 
     System.out.println("Please enter the filename of the file you want to open: "); 

      newFile = keyboard2.next(); 
    }  
} 

他越來越對newFile = keyboard2.next()NoSuchElementException錯誤。我瀏覽了大量的例子,發現有人發佈這個問題,但我還沒有找到解決方案,所以我想我會把我的(可疑的)聲譽放在他的位置上。任何人都知道他如何才能使這個工作?

+0

你有沒有試過kerboard2.nextLine(); ??? – buch11

+0

也有同樣的錯誤。 –

+0

上面的代碼進入無限循環哥們,ctrl + z不會讓你走出第一個函數,我想首先你需要糾正那個...... – buch11

回答

2

控制檯上的EOF字符表示文件結束。這就是掃描儀方法調用投擲NoSuchElementException的原因。

純粹的Java應用程序沒有辦法在EOF標記之後讀取任何內容。


有沒有辦法將其清除?

不在純Java中。從理論上說,你可以從本地代碼中做到這一點,但我懷疑這是老師可以接受的。

+0

有沒有辦法清除它?其中一部分是他必須接受任意數量的投入。老師的例子使用Ctrl-Z來結束輸入,但是他的作業分配讓他要求多一個文件名。 –

+0

最終,他檢查了字符串文字(實際上是「EOF」)的輸入,並且他的代碼正在工作。謝謝。 –

1

我讀你的代碼,我想我找到完全無關,你的問題的錯誤:

 //Checks if file already exists. 
     while(currentFile.exists()) 
     { 
      System.out.println(fileName + " already exists"); 
      System.out.println("Error: To prevent this file from being overwritten please enter another file name"); 
      fileName = keyboard.nextLine();   
     } 

如果currentFile.exists(),你會因爲你不更新currentFile得到一個無限循環的循環,只是fileName

雖然這可能不是您目前主要關注的問題,但您仍應該修復此問題,以避免將來遇到問題。


爲了解決你的問題: 他能趕上NoSuchElementException異常。在他的聲明中,他可以執行任何他想做的事情的聲明:退出方法,結束程序,任何事情!

+1

呼叫良好。我粘貼了他的原始源代碼,它確實有這個錯誤,但是他已經修復了。 –

2

您這裏有一個無限循環:

//Checks if file already exists. 
    while(currentFile.exists()) 
    { 
     System.out.println(fileName + " already exists"); 
     System.out.println("Error: To prevent tis file from being overwritten please enter another file name"); 
     fileName = keyboard.nextLine();   
    } 

currentFile不被新的文件名進行更新。

至於實際的問題,斯蒂芬是正確的,一旦你在createFile方法中的EOF,你不能從鍵盤得到更多的輸入。在這方面,你是對的,EOF沒有消耗。您需要使用其他類型的信號來指示輸入文本的結尾(字符串可能爲"EOF")。