Results 1 to 5 of 5
  1. #1
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default [SOLVED] getting program's math right from file input

    I am taking a Java programming course and need to make a program that takes input from two files and adds and/or subtracts accordingly. I've got the program to compile and run. However, I don't think the math is correct.

    Deposits.txt looks like this:

    100.00
    125.00
    78.92
    37.55
    Withdrawals.txt looks like this:

    29.88
    110.00
    27.52
    50.00
    12.90
    These are the text files that have the input. For the actual program, I have two class, which are called Account and SavingsAccount.

    SavingsAccount.java looks like this:

    Java Code:
    public class SavingsAccount
    {
    	double deposit;
    	double interest = 0.05;
    	double withdrawal;
    	double balance = 500.00;
    	double monthly = interest/12;
    	double value = monthly * balance;
    	double DollarAnnual = value + balance;
    
    	public double getBalance()
    	{
    		return balance;
    	}
    
    	public void setDesposit(double dep)
    	{
    		deposit = dep;
    	}
    
    	public double getDeposit()
    	{
    		return deposit;
    	}
    
    	public void setWithdrawal(double with)
    	{
    		withdrawal = with;
    	}
    
    	public double getWithdrawal()
    	{
    		return withdrawal;
    	}
    
    	public double getInterest()
    	{
    		return interest = DollarAnnual;
    	}
    
    	public double getNewBalance()
    	{
    		balance += deposit;
    		return (interest + balance) - withdrawal;
    	}
    }
    Account.java looks like this:

    Java Code:
    import java.util.*;
    import java.io.*;
    import java.text.*;
    
    public class Account
    {
    	public static void main(String[] args) throws IOException
    	{
    		double accumulator = 0.0;
    		double TotalWithdrawal = 0.0;
    		String filename1 = "Deposits.txt";
    		String filename2 = "Withdrawals.txt";
    		NumberFormat cf = NumberFormat.getCurrencyInstance();
    
    		SavingsAccount account = new SavingsAccount();
    
    		File file1 = new File(filename1);
    		Scanner input = new Scanner(file1);
    
    		while (input.hasNext())
    		{
    
    			double data = input.nextDouble();
    			
    			accumulator += data;
    			
    		}
    
    		account.setDesposit(accumulator);
    
    		input.close();
    
    		File file2 = new File(filename2);
    		
    		Scanner input2 = new Scanner(file2);
    
    		while (input2.hasNext())
    		{
    			double data2 = input2.nextDouble();
    
    			TotalWithdrawal += data2;
    		}
    
    		account.setWithdrawal(TotalWithdrawal);
    
    		input2.close();
    
    		account.getWithdrawal();
    		
    
    		System.out.println("You earned " + cf.format(account.getInterest()) + ", resulting in a balance of " + cf.format(account.getNewBalance()));
    	}
    }
    Is the math really correct?

    Never mind: I found where my problem was.
    Last edited by gotenks05; 02-19-2009 at 10:38 PM. Reason: found problem

  2. #2
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    gotenks05: Please go to the top of this page and click "Thread Tools". Then in the drop-down menu click "Mark This Thread As Solved".
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    usually, this is about when someone (hasn't happened in a while, but usually does when someone has a money question) mentions that it's ill-advised to use floats and doubles to represent money and that you should use ints to represent cents.

  4. #4
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Are you saying that you should or shouldn't use doubles in instead of int for dollars and int for cents?
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    you just use int for cents then convert it to dollars whenever you need it represented as so.

    anyways, i'm sure someone else can explain much better why you should use ints instead of floats/doubles (has to do with inaccuracies of binary converting to decimal, like how $0.10 doesn't have a precise value in binary... i think, which on a large scale can propagate to bigger problems). i haven't found myself dealing with money in my programs, so it hasn't been the focus of my attention.

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  2. Selecting an input file for Reader
    By myazuid in forum New To Java
    Replies: 3
    Last Post: 11-25-2008, 01:21 AM
  3. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-18-2008, 11:51 PM
  4. Taking input from Properties File
    By bugger in forum New To Java
    Replies: 4
    Last Post: 05-07-2008, 09:30 AM
  5. How to call the input value from different file?
    By xiongling in forum Networking
    Replies: 6
    Last Post: 04-07-2008, 10:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •