Java deposit and Withdraw problem
I have written a solution to the following assignment but I wont get any output.
I used System.out.println randomly to see where my program stops and I figured out it is righ after the while (inputFile.hasNext()) method. Any suggestion will be great. Thanks!
build a program that uses notepad or another text editor to create a text file named deposits.txt. The file should contain the following
numbers, one per line:
Next, create a text file named withdrawals.txt. The file should contain the following numbers, one per line:
The numbers in the deposits.txt file are the amounts of deposits that were made to a savings account during the month, and the numbers in the withdrawals.txt file are the amounts of withdrawals that were made during the month. Write a program that creates an instance of the SavingsAccount class that you wrote in the programming challenge 10. The starting balance for the object is 500.00. The program should read the values from the Deposits.txt file and use the object's method to add them to the account balance. The program should read the values from the Withdrawals.txt file and use the object's method to substract them from the account balance. The program should call the class method to calculate the monthly interest, and then display the ending balance and the total interest earned.
Deposits and withdrawals
/** designed by saurav
import java.io.*; // needed for File and IOException
import java.util.Scanner; //imports scanner class
public class DepositWithdraw
public static void main(String args)throws IOException
double intRate ;
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the annual interest rate.
System.out.print("What is the annual interest rate?");
// Create a SavingAccount object.
SavingAccount SavingAccount1= new SavingAccount(bal,intRate);
// Open the Deposits.txt file.
File file = new File ("Deposits.txt");
Scanner inputFile = new Scanner(file);
// Get the deposits from the Deposits.txt file while (inFile.hasNext())
double num1 = inputFile.nextDouble();
//Add the numbers
amount += num1;
//Deposit the file input.
//Close the Deposits.txt file
// Open the Withdrawals.txt file.
//Open Withdrawal file
File file2 = new File("Withdrawal.txt");
Scanner inputFile2 = new Scanner(file2);
// Get the withdrawals from the Withdrawals.txt file while (inFile.hasNext())
double num2 = inputFile2.nextDouble();
inputFile2.close(); // Close the Withdrawals.txt file.
// Get the balance before adding interest.
// Add the interest.
// Calculate the interest earned.
// Display the interest earned and the balance.
System.out.println("Account balance $" + SavingAccount1.getBalance());
System.out.println("Total interest earned $" + SavingAccount1.getLastInterest());
/** designed by Saurav Sharma
public class SavingAccount
private double balance;
private double interestRate;
private double lastInterest;
// SavingsAccount constructor
public SavingAccount( double bal,double intRate)
balance = bal;
interestRate = intRate;
lastInterest = 0.0;
// The withdraw method withdraws an amount from the account.
public void withdraw(double amount)
balance -= amount;
//The deposit method deposits an amount into the account.
public void deposit(double amount)
* The addInterest method calculates the monthly
* interest and adds it to the account balance.
public void addInterest()
// Get the monthly interest rate.
double monthlyInterestRate = interestRate / 12;
// Calculate the last amount of interest earned.
lastInterest = monthlyInterestRate * balance;
// Add the interest to the balance.
balance += lastInterest;
//The getBalance method returns the account balance.
public double getBalance()
* The getInterestRate method returns the annual
* interest rate.
public double getIntrestrate()
//The getLastInterest method returns the last amount of interest earned.
public double getLastInterest()
Re: Java deposit and Withdraw problem
Check your while loop very carefully.