Java 初始化和FileNotFoundException错误
我是一名Java新手,正在编写一个程序,该程序由一个主方法、一个类文件、两个input.txt文件和一个output.txt文件组成。主要方法应该询问用户帐户余额和年利息是多少,并从各自的文件中导入存款和取款信息,然后在输出文件中显示类文件中的所有计算。 我最初写这个文件是为了要求用户使用扫描器输入所有这些信息,现在我正试图让它使用文件作为输入来工作……这并不顺利 主要方法:Java 初始化和FileNotFoundException错误,java,Java,我是一名Java新手,正在编写一个程序,该程序由一个主方法、一个类文件、两个input.txt文件和一个output.txt文件组成。主要方法应该询问用户帐户余额和年利息是多少,并从各自的文件中导入存款和取款信息,然后在输出文件中显示类文件中的所有计算。 我最初写这个文件是为了要求用户使用扫描器输入所有这些信息,现在我正试图让它使用文件作为输入来工作……这并不顺利 主要方法: import java.util.Scanner; import java.io.*; publ
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;
}
}
/**
*@(#)SavingsAccount.java
*
*
*@作者
*@version 1.00 2013/5/6
*/
公共类储蓄帐户{
//变数
私人双重利益;
私人双平衡;
//建造师
公共储蓄账户(双b)
{
平衡=b;
利息=0.0;
}
//访问者
公共利益(双i)
{
利息=i;
}
公共空间退平衡(双b)
{
平衡=b;
}
//突变子
公众利益()
{
利息返还;
}
公共双getBalance()
{
收益余额;
}
//提取方法
公共作废撤销(双重撤销)
{
余额=余额-提取;
}
//存款方法
公共无效存款(双倍存款)
{
余额=余额+存款;
}
//向余额中增加月利息
public void addInterest()
{
双x=((利息/12)*余额);
平衡=平衡+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.
--------------------配置:--------------------
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:43:错误:变量amountdraw可能尚未初始化
amountdraw+=num;
^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:46:错误:变量w可能尚未初始化
w++;
^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:50:错误:变量amountdraw可能尚未初始化
取款(金额取款);
^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:62:错误:变量amountDeposit可能尚未初始化
amountDeposit+=num;
^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:65:错误:变量d可能尚未初始化
d++;
^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:69:错误:变量amountDeposit可能尚未初始化
货币.存款(金额存款);
^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:73:错误:变量filenameOutput可能尚未初始化
oFile=新的PrintWriter(filenameOutput);
^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:75:错误:变量w可能尚未初始化
oFile.println(“提款总额为:“+w”);
^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:76:错误:变量d可能尚未初始化
oFile.println(“宣誓书的总金额为:“+d”);
^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:37:错误:未报告的异常文件NotFoundException;必须被抓住或宣布被抛出
iFile=新扫描仪(wFile);
^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:55:error:unreported exception FileNotFoundException;必须被抓住或宣布被抛出
iFile=新扫描仪(dFile);
^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:73:error:unreported exception FileNotFoundException;必须被抓住或宣布被抛出
oFile=新的PrintWriter(filenameOutput);
^
12个错误
进程已完成。
我不是在找施舍,这是我第一次在这里寻求帮助,但我知道你们不会帮我解决这个问题…我只是需要学习如何正确地做这件事。我只是不知道为什么初始化会出现问题(所有提到的变量都已初始化),并且文件位于同一个文件夹中。从代码的最顶端写下以下内容:
double amountdraw代码>就是这样。这不是一个初始化,而是一个声明
如果你的女朋友开始谈论孩子也是一样的,对吧?如果她宣布“我想要孩子”,这和她“初始化”(生)一个婴儿是不一样的。这两种情况可能压力相同,但方式不同
您需要键入double amountdraw=0.0代码>或在修改值之前添加以下内容:amountdraw=0.0代码>。这将解决您的第一个问题。编辑:对于错误
当您声明这些变量时,应该给它们赋值。i、 e.int w=0
等
肮脏的快速修复:对于其他与异常相关的错误,为了简单起见,您可以将publicstaticvoidmain(String[]args)抛出FileNotFoundException
,以便在过渡期间正常工作
正确的修复:通常,您应该处理FileNotFoun
amountWithdraw = 0;