Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Cannot get right output? Missing something? please help!

  1. #1
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Cannot get right output? Missing something? please help!

    Okay for the life of me I cannot get the right output for my program. I need to get the total number of pennies in dollar amount for the output of Total Pay. This is my program. The directions are:

    Write a program that calculates the amount a person would
    earn over a period of time if his or her salary is one penny the first day,
    two pennies the second day, and continues to double each day. The program should then show the total pay at the
    end of the period. The output should be displayed in a dollar amount, not the number
    of pennies. Do not accept a number less than 1 for the number of days worked.

    import java.util.Scanner;
    import java.text.DecimalFormat;
     
    public class PenniesForPay
    {
    	public static void main(String[] args)
    	{
    		int numDays;
    		int pennies = 1;
    		int day = 1;
    		double totalSalary = 0.01;
     
    		Scanner keyboard = new Scanner(System.in);
     
    		DecimalFormat formatter = new DecimalFormat("#,###.##");
     
    		System.out.print("Please enter the number of days worked: ");
    		numDays = keyboard.nextInt();
     
    		while (numDays < 1)
    		{
    			System.out.print("Enter the number of days worked: ");
    			numDays = keyboard.nextInt();
    		}
    			System.out.println(" ");
    			System.out.println("Day " + " Pennies(earned)");
    			System.out.println("------------------------");
     
    		while (numDays > 0)
    		{
    			System.out.println(day + " = " + " "
    							  + formatter.format(pennies));
     
    			pennies *= 2;
    			totalSalary += pennies / 100;
    			day++;
    			numDays--;
    		}
    			System.out.println(" ");
    			System.out.println("Total pay: $"
    							  + formatter.format(totalSalary));
    	}
    }


    --- Update ---

    totalSalary should be total number of pennies / 100....However its not picking up only day 30 of pennies which is 536,870,912 pennies and then dividing it?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cannot get right output? Missing something? please help!

    The code using integer math: 33/100 = 0
    Change the denominator to float: 33/100.0 = .33
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Cannot get right output? Missing something? please help!

    Okay I tried and I am still getting the same output. I think I am actually thinking about it all wrong. My code is right but i was thinking it should just divide the last number and get my result when it should add all the days together then divide by 100. duh!

    --- Update ---

    Actually the answer is different when not dividing by 100.0.. you are correct Norm. Thanks!

Similar Threads

  1. [SOLVED] What am I missing???
    By Sailorgary1964 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 21st, 2014, 06:50 AM
  2. [SOLVED] 'ELSE' missing 'IF'
    By Andrew Red in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 30th, 2013, 11:51 AM
  3. What am I missing?
    By jean28 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2012, 10:49 AM
  4. how could I output to a text area the output of a method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 12th, 2012, 07:49 PM
  5. What am I missing?
    By prgmrGrl in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 10th, 2012, 12:33 AM