2016-09-21 62 views
-1

我正在編寫一個程序,可以使用傳統的加密方法對消息進行編碼和解碼。該消息將從文件讀入並寫入輸出文件。下面的程序是用java編寫的,並且沒有錯誤編譯。當我運行程序來測試它是否在我給它輸入和輸出文件的名稱後運行時,它會運行成某種異常拋出錯誤。我認爲問題在於代碼中的for循環。這是一個將所有消息存儲到字符數組中的循環。任何建議,以解決它或另一個更好的工作的數據結構(如堆棧或隊列)?爲什麼當它試圖從文件讀取時出現錯誤?

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

class CryptoProject1 
{ 
static char eord; 
static Scanner cin=new Scanner(System.in); 
static char [] message=new char[10000]; 

public static void main (String [] args) 
    throws IOException 
{ 
    //getting the input txt file name from user 
    String infilename; 
    System.out.println("Please give the name of the input file."); 
    infilename=cin.nextLine(); 
    Scanner fileread=new Scanner (new FileReader(infilename)); 

    //getting the output txt file name from user 
    String outfilename; 
    System.out.println("Please give the name of the output file."); 
    outfilename=cin.nextLine(); 
    PrintWriter filewrite=new PrintWriter(new FileWriter(outfilename)); 

    //saving the message into an array 
    //construct/make it into a usable function?? 
    for(int i=0; i<message.length; i++) 
    { 
     message[i]=fileread.next().charAt(0); 
    } 

    //trial to make sure it reads and writes correctly 
    //printing the message onto the output file 
    for(int i=0; i<message.length; i++) 
    { 
     filewrite.print(message[i]); 
    } 

} 
+2

「它運行到某種的例外「。什麼是例外?它應該告訴你到底發生了什麼問題,以及在哪裏。 – resueman

+0

在你的第一個循環中添加一個檢查:if(fileread.hasNext()),然後執行動作else break – Angen

+2

@resueman這是它給出的錯誤信息:線程「main」中的異常java.util.NoSuchElementException at java .util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1416) at CryptoProject1.main(CryptoProject1.java:112) –

回答

2
for(int i=0; i<message.length; i++) 
{ 
    message[i]=fileread.next().charAt(0); 
} 

在這裏,你可以不知道,如果文件長度大於消息相同或更高,文件也可以具有10個字符。你需要這樣做:

for(int i=0; i<message.length; i++) 
{ 
    if(!fileread.hasNext()) 
    break; 
    message[i]=fileread.next().charAt(0); 
} 

只是一個簡單的檢查,如果還有東西要從文件中讀取,如果沒有,然後停止閱讀。

另外,習慣上使用Java對象File來表示文件,而不是使用字符串來保存文件路徑。例如:

private File output; 
public void create file(String path) 
{ 
    output = new File(path); 
} 

private BufferedWriter out = new BufferedWriter(new FileWriter(output)); 
+0

修復了錯誤,但現在當它寫入輸出文件只需要每個單詞的第一個字母,然後是一串空值。 –

+0

嗯,你正在閱讀文件字符char,並且它很難與掃描儀('下一步()'採取下一個標記,你正在從它的第一個字符)。 – Shadov

+0

我的建議是:將'message'更改爲字符串列表'ArrayList '並將'fileread.nextLine()'添加到它。這需要很少的修改(你不能使用它的大小來讀取文件,因爲在創建之後它的大小是0,你需要while循環,比如'while(fileread.hasNext())message.add(fileread.nextLine())' 。然後你可以使用列表的size()來寫入文件和循環。[勾選這個參考](https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList。 html) – Shadov

0

此外,當您寫入一個文件,請確保您調用close()方法關閉它或它的內容將不會被保存

+0

Oh yeah ...沒錯!謝謝!但它仍然做同樣的事情... –

+0

同樣的事情? –

+0

是的,這意味着可能有一個問題在循環中讀取字符並將其分配給數組。它可能與.charAt(0)有關。 –

0
//I ran this code without an error 
//getting the input txt file name from user 
String infilename; 
System.out.println("Please give the name of the input file."); 
infilename=cin.nextLine(); 
Scanner fileread=new Scanner (new FileReader(infilename)); 

//getting the output txt file name from user 
String outfilename; 
System.out.println("Please give the name of the output file."); 
outfilename=cin.nextLine(); 
PrintWriter filewrite=new PrintWriter(new FileWriter(outfilename)); 

//saving the message into an array 
//construct/make it into a usable function?? 
for(int i=0; i<message.length; i++) 
{ 
    if(fileread.hasNext()) 
    message[i]=fileread.next().charAt(0); 
} 
fileread.close(); 

//trial to make sure it reads and writes correctly 
//printing the message onto the output file 
for(int i=0; i<message.length; i++) 
{ 
    filewrite.print(message[i]); 
} 
filewrite.close(); 
} 
+0

我確保我的代碼完全符合這一點,它仍然只給出每個單詞的第一個字母和輸出中的一堆空值...我不是現在編譯有問題,我有麻煩讓它按我想要的方式工作。 –

相關問題