我一直收到这个程序的错误。在编译器中获取日期后出现错误提示,我不确定如何修复它

I keep getting a error with this program. After getting the date in the compiler the error prompts and I'm unsure as to how to fix it

这是我必须做的作业

Design a class named Account that contains:

A private int data field named id for the account (default 0).

A private double data field named balance for the account (default 0).

A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate.

A private Date data field named dateCreated that stores the date when the account was created.

A no-arg constructor that creates a default account.

A constructor that creates an account with the specified id and initial balance.

The accessor and mutator methods for id, balance, and annualInterestRate.

The accessor method for dateCreated.

A method named getMonthlyInterestRate() that returns the monthly interest rate.

A method named getMonthlyInterest() that returns the monthly interest.

A method named withdraw that withdraws a specified amount from the account.

A method named deposit that deposits a specified amount to the account.

Implement the class. (Hint: The method getMonthlyInterest() is to return monthly interest, not the interest rate. Monthly interest is balance * monthlyInterestRate. monthlyInterestRate is annualInterestRate / 12. Note that annualInterestRate is a percentage, e.g., like 4.5%. You need to divide it by 100.)

Write a test program that creates an Account object with an account ID of 1122, a balance of ,000, and an annual interest rate of 4.5%. Use the following transactions (negative values are withdrawals, positive values are deposits). I would like to see the transactions stored in a file.

-100.00
250.00
650.00
-25.00
10.00
-50.00
-60.00
-80.00
100.00 

Print the balance, the monthly interest, and the date when this account was created. Calculate the interest based upon the ending balance at the end of the month, you should include the interest in your final balance.

下面列出了我目前所拥有的。任何帮助,将不胜感激。错误作为注释列在代码之后。 以下是我目前所拥有的:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Date;
import java.util.Scanner;

//Account
class Account
{
//variables
private int id;
private double balance;
private double annualInterest;
private Date dateCreated;

 //default constuctor
Account(){}

//constructor
Account(int i,double bal){
this.id=i;
this.balance=bal;
}
//set Id
public void setId(int i)
{
this.id=i;
}
//set Balance
public void setBalance(double bal)
{
this.balance=bal;
}
//set InterestRate
public void setInterestRate(double rate)
{
this.annualInterest=rate;
}


//get ID
public int getId()
{
return id;
}
//get Balance
public double getBalance()
{
return balance;
}
 //get InterestRate(
public double getInterestRate()
{
return annualInterest;
}
//get Date
public Date getDate()
{
dateCreated=new Date();
return dateCreated;
}
//get MonthlyInterestRate
public double getMonthlyInterestRate()
{
double monthlyInterest=getMonthlyInterest();
return balance*monthlyInterest;
}
//get MonthlyInterest
public double getMonthlyInterest()
{
return (annualInterest/1200);
}
//withdraw
public void withdraw(double amt)
{
this.balance=balance-amt;
}
//deposit
public void deposit(double amt)
{
this.balance=balance+amt;
}
}
//main method
public class AccountDriver {


public static void main(String[] args) throws FileNotFoundException {

//set account id, balance 
Account at=new Account(1122,20000);

//set InterestRate
at.setInterestRate(4.5);


System.out.println("ID: "+at.getId());
System.out.println("Balance: "+at.getBalance());
System.out.println("Interest: "+at.getInterestRate());
System.out.println("Date: "+at.getDate().toString());


//transaction
Scanner sc=new Scanner(new File("c:\books\trans.txt"));

while(sc.hasNext())
{
int amt=sc.nextInt();
if(amt<0)
at.withdraw(amt);
else
at.deposit(amt);
}
//print balance
System.out.println("Balance After Trasaction: ");
System.out.println("Balance: "+at.getBalance());
System.out.println("Interest Rate: "+at.getMonthlyInterestRate());

}

}

//The error I keep receiving is below. 
//Exception in thread "main" java.util.InputMismatchException
//at java.base/java.util.Scanner.throwFor(Scanner.java:939)
//at java.base/java.util.Scanner.next(Scanner.java:1594)
//at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
//at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
//at Personal_Projects.AccountDriver.main(AccountDriver.java:107)

您正在从文件“c:\books\trans.txt”读取数据。

根据您的代码,

while(sc.hasNext()) {
     int amt=sc.nextInt();
     if(amt<0)
        at.withdraw(amt);
     else
        at.deposit(amt);
}

您只是从文件中读取整数,意味着文件应该是这样的

1
2
3
4

以此类推,没有任何字符串或逗号什么的,如果有任何其他字符串或逗号将抛出异常。您可以通过在 while 循环中使用 sc.hasNextInt() 来解决错误。

** 解决方案**

while(sc.hasNextInt()) {
     int amt=sc.nextInt();
     if(amt<0)
        at.withdraw(amt);
     else
        at.deposit(amt);
}