Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# calculate amount due

Greenhorn
Posts: 11

Need to find out how long it would take to pay the due amount but for some reason, year is always 1 no mater what i do :|

Sheriff
Posts: 3015
12
I would guess that "paid" is greater than zero from the start, so you never enter that loop. I'm not sure why that would be, but I do notice that your calculation is done with different variables from the ones you pass in as parameters. That's probably a source of some of the confusion.

Rancher
Posts: 42975
76
What is "payment"? What is "interest"? Those are not defined in this method, so it's impossible to argue about it: PostRealCode

mike sin
Greenhorn
Posts: 11
should be fixed now and should add that return should be the amount of time it takes for paid in years

lowercase baba
Bartender
Posts: 12613
50
I think even if you go into that loop, you'll never get out.

paid never changes inside the loop.

mike sin
Greenhorn
Posts: 11
Hmm Even If I do
total += (paid++);
(just an example) year is still 1 and always remains one :S

Greg Charles
Sheriff
Posts: 3015
12
Again, paid > 0, so you never enter your loop. I don't know the values you're passing into the method, or where "debt" comes from, but I'm sure that's true.

you should talk through in plain language what you are trying to do. It's always best to get your thoughts straight before coding. Are you computing how many years it would take to pay off a given debt? That would be when remaining debt is zero, not when the paid amount is more than zero. Think about how you'd solve the problem on paper first, then try to make a program to do it.

mike sin
Greenhorn
Posts: 11
Hmm, This is what I am basically trying to do
Debt - \$1500
internet - 15%
payment - \$100/mnth
end of year - (1500*0.15)+1500
payment at end of year = 100(12)
debt left(525) = end of year(1725) - payment at end of year(1200)
so it takes 1 year for all debt to pe paid.

Ranch Hand
Posts: 72
What i should tell you is....
until and unless you decrement the variable "paid",
you will never get into that loop...

So that if "paid" is positive at first iteration then it goes into the loop...
then year is will increment... and "paid" is getting decremented... still
condition is true.... It should work like this....

Marshal
Posts: 58421
178
1 year? It looks like more than 1 year to me.

mike sin
Greenhorn
Posts: 11
Hey,
The calculation of debt is done at the end of the year so it should never reach 2nd year hence why it only takes one year.
I think the only trouble I have is this:
for(year = 1; paid>=0; year++) {
paid -= (year);
}
not sure how to do the for
Thanks!

Ranch Hand
Posts: 710

mike sin wrote:Hmm, This is what I am basically trying to do
Debt - \$1500
internet - 15%
payment - \$100/mnth
end of year - (1500*0.15)+1500
payment at end of year = 100(12)
debt left(525) = end of year(1725) - payment at end of year(1200)
so it takes 1 year for all debt to pe paid.

If it took 1 year to pay all the debt, the debt left would be zero I would think. Even with no interest, 100 a month won't pay off a 1500 debt. I'm having trouble understanding where you come up with 1 year for debt to be paid off?

Campbell Ritchie
Marshal
Posts: 58421
178
You need some very specialised pieces of hardware to work out your problem.
• A pencil
• A sheet of paper
• An eraser. Preferably a large one
•
mike sin
Greenhorn
Posts: 11
ahhh okay
i see it now,
the end will be finished in the starting/middles of 1st year but the computer will find out its done after 2nd year so 2 years to pay the debt off as we are working with just years and not months and etc.

mike sin
Greenhorn
Posts: 11

Here's my try with do-while loop, year always returns 2 for some reason the loop only occurs once argh :/

W. Joe Smith
Ranch Hand
Posts: 710

mike sin wrote:

Here's my try with do-while loop, year always returns 2 for some reason the loop only occurs once argh :/

I think your first problem is you mean while(debt>=0), since <0 would mean you have negative debt. Also, you aren't changing the amount of debt in your loop, so whatever debt you have going in is what will always be there.