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!

my assignment:

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
import java.text.DecimalFormat;

public class DepositWithdraw

public static void main(String[] args)throws IOException

      double intRate ;
      double interestEarned;
     double amount=0.00;
     double bal=500.00;

      // 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?");

     intRate= keyboard.nextDouble();

      // 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())
while (inputFile.hasNext());
			//Read numbers
			 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())

      while (inputFile2.hasNext());
	  			//Read numbers
	  			double num2 = inputFile2.nextDouble();

	  			amount-= num2;

      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());
}//end class

savingacount class:

/** 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()
	return balance;


    * The getInterestRate method returns the annual
    * interest rate.
    public double getIntrestrate()
		return interestRate;


   //The getLastInterest method returns the last amount of interest earned.

    public double getLastInterest()
		return lastInterest;



Well, the syntax for while is


There should be no ";" after the while statement.

But at line 44 and 76 u have added a ";" to the while statement. What this does is, it makes the while condition to keep executing....until breaks. which it wont do in your case because you are not reading anything from the file (the body is not executed) and hence the file will always have something.

So remove those semicolons.

This article has been dead for over six months. Start a new discussion instead.