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

Delta

This is what I have to do for an assignment

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 $20,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.

What I have so far is listed below. Any help would be appreciated. The error is listed after the code as comments. Below is what I have so far:

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)
Rehan Javed

You are reading data from file "c:\books\trans.txt".

According to your code,

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

You are only reading integers from the files, means files should be like this

1
2
3
4

And so on, no any string or comma or anything, if there is any other string or comma it will throw the exception. You can resolve the error by using sc.hasNextInt() in while loop.

** Solution**

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Keep getting mySQL if statement syntax error. How do i fix it?

I keep getting a compiler error whenever I try to use functions

How do i fix this error i'm getting "Sorry, the page you are looking for could not be found."

How can i fix this, my login page wont connect and i keep on getting this error

I'm getting this annoying error on this easy line and how can I fix that? | Discord.js

I'm getting error in my program

I'm working with R. I am performing a k-NN analysis on the training data, but I keep getting an error on my knn function. How can I fix this

I keep getting DNS_PROBE_FINISHED_BAD_CONFIG error. How can I fix this?

how do i add this list together? I keep on getting an error

Getting an error running this program as a command line argument in Java, how can I fix this?

I keep getting a -1700 error

I keep getting a compiler error saying css no valid after double and triple checking I can't seem to find the error

After successful(?) installation of ask-cli, I'm getting a deployment error where it says invalid json. How do I fix this?

how to fix this error and why am I getting it?

Python: I keep getting a broadcasting error, but I'm not sure how to fix it

I keep getting an error in my discord bot, and cannot figure out how to fix it

I keep getting this error and i can't seem to fix it

Why am I getting an attribute error? How can I fix it?

How do I fix return error I'm getting from this generator code?

How do I fix this error that I'm getting while making a POST request in nodejs

I'm getting a segmentation fault error in my program, but it is unclear how

Why am I getting a JSON error and how do I fix it

I'm keep getting REF error in all the cells that I paste the data to after writing the formula

I keep getting the error message AttributeError: 'pandas._libs.properties.AxisProperty' object has no attribute 'unique'. How should I fix this error?

why do i keep getting this error and how do i solve it

I'm getting an error in the Unity Editor saying that I have multiple plugins. How can I fix this?

keep getting the same error, how do I fix?

I'm new to flutter and I'm keep getting this error

why am I getting this error in AppleScript and how do I fix it