2013-12-15 146 views
0

我是一個新的Java學生,正在編寫一個由主方法,類文件,兩個輸入.txt文件和一個output.txt文件組成的程序。主要方法應該詢問用戶帳戶餘額和年度利息是什麼,並從他們各自的文件中導入存取款信息,然後在輸出文件中顯示類文件中的所有計算。 我原本寫這個文件,要求用戶使用掃描儀的所有這些輸入,現在我試圖讓它使用文件作爲輸入工作...這不是很好。初始化和FileNotFoundException錯誤

主要方法:

import java.util.Scanner; 
    import java.io.*; 
    public class SavingsAccountDemo { 

    public static void main(String[] args) { 
    //declare variables 
    double interest; 
    double startAmount; 
    double amountDeposit; 
    double amountWithdraw; 
    double d, w; 
    String filenameInputW, filenameInputD, filenameOutput; 
    PrintWriter oFile; 
    File wFile, dFile; 
    Scanner iFile; 

    Scanner key = new Scanner(System.in); 

    //get initial balance 
    System.out.println("Enter initial account balance: "); 
    startAmount = key.nextDouble(); 

    //create SavingsAccount class object sending starting amount to constructor 
    SavingsAccount money = new SavingsAccount(startAmount); 

    //get annual interest rate 
    System.out.println("Enter annual interest rate: "); 
    interest = key.nextDouble(); 

    //send interest rate to class 
    money.setInterest(interest); 

    //Retrieve withdrawals from withdrawal.txt 
    filenameInputW="withdrawal.txt"; 
    wFile = new File (filenameInputW); 
    iFile = new Scanner (wFile); 
    while(iFile.hasNext()) 
    { 
     double num; 

     num=iFile.nextDouble(); 
     amountWithdraw += num; 

     if(amountWithdraw >= 0.1) 
     w++; 
    } 

    //send to SavingsAccount class 
    money.withdraw(amountWithdraw); 

    //Retrieve deposits from deposit.txt 
    filenameInputD="deposit.txt"; 
    dFile = new File (filenameInputD); 
    iFile = new Scanner (dFile); 

    while(iFile.hasNext()) 
    { 
     double num; 

     num=iFile.nextDouble(); 
     amountDeposit += num; 

     if (amountDeposit >= 0.1) 
     d++; 
    } 

    //send to SavingsAccount class 
    money.deposit(amountDeposit); 

    //display retults 
    filenameInputW="output.txt"; 
    oFile=new PrintWriter (filenameOutput); 
    oFile.println("The ending balance is: " + money.getBalance()); 
    oFile.println("The total amount of withdrawls are: " + w); 
    oFile.println("The total amount of deposists are: " + d); 
    oFile.println("The annual interest rate is: " + money.getInterest()); 

} 

}

我的類文件

 
/** 
* @(#)SavingsAccount.java 
* 
* 
* @author 
* @version 1.00 2013/5/6 
*/ 


public class SavingsAccount { 

     //variables 
     private double interest; 
     private double balance; 


     //Constructor 
     public SavingsAccount(double b) 
     { 
      balance = b; 
      interest = 0.0; 
     } 

     //Accessors 
     public void setInterest(double i) 
     { 
      interest = i; 
     } 

     public void setBalance(double b) 
     { 
      balance = b; 
     } 

     //Mutators 
     public double getInterest() 
     { 
      return interest; 
     } 

     public double getBalance() 
     { 
      return balance; 
     } 

     //Withdraw method 
     public void withdraw(double withdraw) 
     { 
      balance = balance - withdraw; 
     } 

     //Deposit method 
     public void deposit(double deposit) 
     { 
      balance = balance + deposit; 
     } 


     //Adding monthly interest to the balance 
     public void addInterest() 
     { 
      double x = ((interest/12) * balance); 
      balance = balance + x; 
     } 

} 

我得到這些錯誤:

--------------------Configuration: -------------------- 
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:43: error: variable amountWithdraw might not have been initialized 
      amountWithdraw += num; 
      ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:46: error: variable w might not have been initialized 
      w++; 
      ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:50: error: variable amountWithdraw might not have been initialized 
     money.withdraw(amountWithdraw); 
        ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:62: error: variable amountDeposit might not have been initialized 
      amountDeposit += num; 
      ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:65: error: variable d might not have been initialized 
      d++; 
      ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:69: error: variable amountDeposit might not have been initialized 
     money.deposit(amountDeposit); 
        ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:73: error: variable filenameOutput might not have been initialized 
     oFile=new PrintWriter (filenameOutput); 
          ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:75: error: variable w might not have been initialized 
     oFile.println("The total amount of withdrawls are: " + w); 
                  ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:76: error: variable d might not have been initialized 
     oFile.println("The total amount of deposists are: " + d); 
                  ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:37: error: unreported exception FileNotFoundException; must be caught or declared to be thrown 
     iFile = new Scanner (wFile); 
      ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:55: error: unreported exception FileNotFoundException; must be caught or declared to be thrown 
     iFile = new Scanner (dFile); 
      ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:73: error: unreported exception FileNotFoundException; must be caught or declared to be thrown 
     oFile=new PrintWriter (filenameOutput); 
      ^
12 errors 

Process completed. 

我不是在尋找施捨,這是我的第一個職位在這裏,但我知道你們不會爲我解決這個問題......我只需要學習如何正確地做到這一點。我只是不知道爲什麼初始化有問題(所有提到的變量都已初始化)並且這些文件位於同一個文件夾中。

+1

代替意味着filenameOutput="output.txt";你覺得呢'變量d可能沒有initialized'手段? –

回答

0

我認爲你對初始化和聲明之間的區別感到困惑。給出錯誤的變量確實被聲明瞭,但它們沒有初始值。請參閱w++。這意味着:將1加到w。但w是什麼?你沒有定義那個。

同樣的問題是真實的爲damountWithdrawamountDeposit

而且,用文件作爲輸入新的掃描儀需要一個FileNotFoundException一個try-catch語句。

最後,我想你在filenameInputW="output.txt";

+0

謝謝你爲我清理這個區別並指出'filenameInputW' – blockasmash

+0

沒問題。很高興我能幫助你。祝你學習愉快! –

0

從您的代碼中,您可以在頂部輸入以下內容:double amountWithdraw;就是這樣。這不是初始化,而是一個聲明。

如果你的女朋友開始談論孩子,這同樣適用嗎?如果她宣佈「我想要孩子」,這與她「初始化」(生下一個孩子)並不相同。兩者都可能同樣有壓力,但方式不同。

您需要輸入double amountWithdraw = 0.0;或在修改該值之前添加以下內容:amountWithdraw = 0.0;。這將解決您的一些問題的第一個。

0

編輯:對於過錯......

你應該在聲明他們給這些變量的值。即int w = 0

髒速戰速決:至於其他異常相關的錯誤,爲了簡單起見,你可以把public static void main(String[] args) throws FileNotFoundException得到的東西的臨時工作。

正確修復:通常,雖然,你應該處理在catch塊FileNotFoundException,因爲它不是好的做法重新拋出異常慎之又慎。

+0

他們的作用範圍很好。 –

+0

你;絕對正確,我站在糾正,這是遲到了;-) – splrs

+0

這就是爲什麼你可以編輯:) –

0

--------------------Configuration: -------------------- C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:43: error: variable amountWithdraw might not have been initialized amountWithdraw += num;

嗯,你聲明的變量amountWithdraw爲雙,但compilator不知道什麼是該變量的值,他可以不加民。所以聲明後你需要初始化amountWithdraw。它可以是這樣的:

amountWithdraw = 0; 

這很簡單。

+0

當你知道該怎麼做時,一切都很簡單。 MVC對我來說很簡單 - 繼續解釋它是如何適用於新的...;) – Izmaki

+0

它就像基礎) – user2167382