Results 1 to 6 of 6

Thread: Java Newbie Needs Help

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    9

    Java Newbie Needs Help

    Hi there, I am fairly new to Java and am doing my best to work with it, but I am having a major problem with one of my programs.

    What the program does is, the user will enter an initial balance and a rate of interest to be compounded annually. The program then returns the balance the user will have at the end of each year until it reaches ten years.

    However, according to my adventures in grade 12 math, the results are seriously wrong. Here is my code:

    Code:
    import java.io.*;
    
    public class CompoundInt {
        
        public static void main(String[] args) throws IOException 
        	{
    		
    			BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));    	
    		
    			System.out.print("What is the initial balance?" + '\t');
    			String initialBalance = myInput.readLine();
    			double inBalance = Double.parseDouble(initialBalance);
    			
    			System.out.print("What is the rate of interest(annually compounded)?" + '\t');
    			String stringRateOfInterest = myInput.readLine();
    			double rateOfInterest = Double.parseDouble(initialBalance);	
        	
        		System.out.println(" ");
        		
        		compound(inBalance, rateOfInterest);
        	}
        static public void compound (double deposit, double rate) 
        {
        		double endDeposit;
        		int loopNumber = 0;
        		System.out.println("Year" + '\t' + '\t' + "Final Balance");
        		rate = 1 + (rate / 100);
        		
        		while (loopNumber < 10)
        		{
        			loopNumber++;
        			endDeposit = (deposit * (Math.pow(rate, loopNumber)));
        			System.out.println("Year " + loopNumber + ":" + '\t' + endDeposit);		
        		}
        }    
    }
    EDIT: I don't know how clear it may be in the code, but the output is totally wrong. For example, say that the initial balance is 100 and the rate of interest is 5%. According to my EXTENSIVE calculations, at the end of year 1, the user should have $105. The next year they should have $110, etc. However, according to this program, the user will have 200 the first year, 400 the next, 800 the third year, etc.
    Last edited by lonlonmalon; Feb 25th, 2007 at 11:50 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
  •  



Click Here to Expand Forum to Full Width