This week's giveaway is in the Java/Jakarta EE forum.We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line!See welcome thread
Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Greenhorn
Posts: 2
hello, I am a little stuck on a homework assignment, "Pennies for Pay" basically i have to write a program using a loop that will calculate a persons earnings over a period of time. The person will earn a penny the first day, 2 pennies the second day and continues to double each day and show a table displaying the salary for each day and total pay at the end of the period... My problem is the output i am getting is different than it should be see below:

run:
How many days did you work?
5
Days Worked Amount Paid
--------------------------------------------
1 0.02 <--This should be 1
2 0.04
3 0.08
4 0.16
5 0.32
\$1.62
BUILD SUCCESSFUL (total time: 3 seconds)

Ranch Hand
Posts: 304
I edit because my answer was not correct.

To see why the result in the first case is 0.02 instead of 0.01 you have to check what are you doing in line 45. What value has currentpay in the first loop? and what value are you asigning? maybe and aditional check is neccesary there ;)

Marshal
Posts: 57482
175
I can see what has happened, but you will need additional information to see it yourself.

Insert the following line in your code, after line 44 and before line 45:and this after the present line 47:Note the two lines are very similar

Why are you using DecimalFormat rather than the printf methods? What will happen if somebody enters 0 as number of days worked?

Albareto McKenzie
Ranch Hand
Posts: 304
Ey Campbell, you are fast, I was editting my answer

If someone inserts 0 there is a check at line 35

Campbell Ritchie
Marshal
Posts: 57482
175

Albareto McKenzie wrote:Ey Campbell, you are fast, I was editting my answer

If someone inserts 0 there is a check at line 35

Speed? It's all practice, practice

But don't tell Jessica what the check at line 35 actually does

Albareto McKenzie
Ranch Hand
Posts: 304
She will find it soon

 You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS