Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Java
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 03-20-2005, 01:13 PM   #1 (permalink)
rockybalboa
Registered User
 
Join Date: Mar 2005
Posts: 6
rockybalboa is on a distinguished road
Can't get loop to work

Hello,
Can anyone advise me on why I can't get the following code to loop? I'm trying to get continuous calculation until the variable amount = 0.
I will work out display params after I get the loop to work. I'm new to Java, but not coding. This is a simple program so there's probably a simple answer.
My thanks to anyone willing to teach me.
Thanks
DW

Code:
import java.text.*; class Wk3 { public static void main (String[] arguments) //create main { int term = 30; //init var for length of loan double amount = 200000; //init var for loan amt double pmt = 0; //init var for payment double rate = .0575; //init var for interest rate double rateMo = 0; //init var for monthly interest paid { rate = (rate/12); //divide annual rate to get monthly rate term = (term * 12); //multiply years to get length in months pmt= (amount * (rate)) / (1-Math.pow(1 + rate, - term)); //compute payment { if (amount > 0) //run if counter >0 { java.text.DecimalFormat dec = new java.text.DecimalFormat(",###.00"); //format next output to dollars.cents System.out.println("\nThis months payment is $" + dec.format (pmt)); //display payment System.out.println ("\nYour loan balance is $" + dec.format (amount)); //display loan amt System.out.println("\nThis months interest is $" + dec.format (amount * (rate ))); //montly interest payment } amount -= pmt; } } } }
__________________
rockybalboa is offline   Reply With Quote
Old 03-20-2005, 01:31 PM   #2 (permalink)
nimaj
Registered User
 
Join Date: Jan 2004
Location: Poughkeepsie, NY
Posts: 18
nimaj is on a distinguished road
Send a message via AIM to nimaj Send a message via Yahoo to nimaj
Why aren't you using a while loop? while (amount != 0) { statements; }
__________________
nimaj is offline   Reply With Quote
Old 03-20-2005, 01:44 PM   #3 (permalink)
rockybalboa
Registered User
 
Join Date: Mar 2005
Posts: 6
rockybalboa is on a distinguished road
Tried while

Thank you for giving me a hand,
I changed to a while loop and it loops ok, but it's not decrementing so it just keeps running.
Any ideas?
__________________
rockybalboa is offline   Reply With Quote
Old 03-20-2005, 01:53 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
can you post your while loop? it seems like the only way the amount variable would not decrement is if the payment was 0.
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 03-20-2005, 02:54 PM   #5 (permalink)
rockybalboa
Registered User
 
Join Date: Mar 2005
Posts: 6
rockybalboa is on a distinguished road
It gets weirder

After taking your advice and a little more searching I got the following code that runs and decrements great. Suddenly I realize that my algorithm isn't working. It's supposed to calculate the loan over 360 months (30 years), but my code runs the pricipal to zero in 189 months. And it looks like the reason is that it is calculating the interest wrong.
I'm about to lose my mind on this project.

Code:
import java.text.*; class Wk3 { public static void main (String[] arguments) //create main { int term = 30; //init var for length of loan double rate = .0575; //init var for interest rate double amount = 200000; //init var for loan amt double pmt = 0; rate = (rate/12); //divide annual rate to get monthly rate term = (term * 12); //multiply years to get length in months pmt= amount * ((rate) / (1-Math.pow(1 + rate, - term))); //compute payment while (amount > 0 & term > 0 ) //run if counter >0 { java.text.DecimalFormat dec = new java.text.DecimalFormat(",###.00"); //format next output to dollars.cents System.out.println("\nThis months payment is $" + dec.format (pmt)); //display payment System.out.println ("\nYour loan balance is $" + dec.format (amount)); //display loan amt System.out.println("\nThis months interest is $" + dec.format (amount * (rate ))); //montly interest payment System.out.println("\nThis months term is " + (term)); term --; amount -= pmt; } } }
__________________
rockybalboa is offline   Reply With Quote
Old 03-20-2005, 03:54 PM   #6 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
Arg, C style Java . . .

Anyways, stylistic comments aside, you're using "&" in your while loop. This doesn't do what you think it does. You want "&&", which is a boolean AND. "&" is a binary AND.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA: My Outlook VBA rule code does't work :( gicio Visual Basic 6 1 11-25-2003 05:16 AM
This should work so why doesn't it ntws01 Standard C, C++ 18 08-27-2003 01:24 PM
Breaking out of external loop agh Standard C, C++ 7 04-30-2003 08:03 PM
Getting X to work in Debian w00t Linux / BSD / OS X 7 08-25-2002 01:44 PM
the endless loop loop loop loop loop loop ... abc123 All Other Coding Languages 2 08-14-2002 03:43 PM


All times are GMT -8. The time now is 08:28 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle