我是一個新的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.
我不是在尋找施捨,這是我的第一個職位在這裏,但我知道你們不會爲我解決這個問題......我只需要學習如何正確地做到這一點。我只是不知道爲什麼初始化有問題(所有提到的變量都已初始化)並且這些文件位於同一個文件夾中。
代替意味着
filenameOutput="output.txt";
你覺得呢'變量d可能沒有initialized'手段? –