Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java deposit and Withdraw problem

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!

    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:
    100.00
    124.00
    78.92
    37.55

    Next, create a text file named withdrawals.txt. The file should contain the following numbers, one per line:
    29.88
    110.00
    27.52
    50.00
    12.90

    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");
     
    System.out.println("works1");
    		Scanner inputFile = new Scanner(file);
     
     
     
          // Get the deposits from the Deposits.txt file while (inFile.hasNext())
    while (inputFile.hasNext());
    		{
    			System.out.println("works1");
    			//Read numbers
    			 double num1 = inputFile.nextDouble();
     
    			//Add the numbers
    			amount += num1;
     
    		}
     
    		System.out.println("works2");
     
           //Deposit the file input.
    		SavingAccount1.deposit(amount);
     
    		//Close the Deposits.txt file
    		inputFile.close();
     
     
     
     
          // Open the Withdrawals.txt file.
    //Open Withdrawal file
    		File file2 = new File("Withdrawal.txt");
    		Scanner inputFile2 = new Scanner(file2);
     
     
    System.out.println("works3");
     
          // Get the withdrawals from the Withdrawals.txt file while (inFile.hasNext())
     
          while (inputFile2.hasNext());
    	  		{
    	  			//Read numbers
    	  			double num2 = inputFile2.nextDouble();
     
    	  			//Subtract
    	  			amount-= num2;
    	  		}
    System.out.println("works4");
     
          inputFile2.close();      // Close the Withdrawals.txt file.
     
          // Get the balance before adding interest.
          SavingAccount1.getBalance();
     
          // Add the interest.
    SavingAccount1.addInterest();
     
          // Calculate the interest earned.
     
    SavingAccount1.getLastInterest();
     
    System.out.println("works5");
          // 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)
      {
    	  balance+=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;
     
    	}
     
     
     
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,468
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Java deposit and Withdraw problem

    while (inputFile.hasNext());
        {
    Check your while loop very carefully.
    Improving the world one idiot at a time!

Similar Threads

  1. Java split problem..
    By arch in forum Java SE APIs
    Replies: 5
    Last Post: August 11th, 2011, 07:48 AM
  2. Java certificate of deposit calculator project
    By jimbo357 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 17th, 2011, 02:18 AM
  3. HTK files from Java problem.
    By ivanloes in forum Java Theory & Questions
    Replies: 0
    Last Post: December 17th, 2010, 05:49 PM
  4. [SOLVED] Java Swing problem?
    By nasser in forum AWT / Java Swing
    Replies: 2
    Last Post: July 3rd, 2010, 12:34 PM
  5. java problem
    By Mj Shine in forum Java Theory & Questions
    Replies: 1
    Last Post: August 14th, 2009, 12:24 AM