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 5 of 5

Thread: Please Help!

  1. #1
    Junior Member
    Join Date
    Feb 2019
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Please Help!

    Okay, I'm a total noob at java and am writing a simple program for a class. The program works,
    but not as I want it to. I really need it to display the total decimals for the totalProfit and totalCost outputs. Also, if it's
    not too much trouble I'd like to know what I'm doing wrong.

    ***********HERE IS THE CODE***********
    // Created By: Russell on 02/09/2019
    // Program that acquires input of total amount of milk, then calculates and 
    // displays the amounts of cartons produced, cost to create the milk and 
    // total profits gained
    package exercise14;
     
    import java.util.*;
     
    public class exercise14 {
        static Scanner console = new Scanner(System.in);
            //declare constants
        static final double CARTON = 3.78;
        static final double COST = 0.38;
        static final double PROFIT = 0.27;
     
        public static void main(String[] args) {
     
            //declare variables        
            int liter;
            int cartonsNeeded;
            double totalCost;
            double totalProfit;
     
            //obtain information
            System.out.println("Enter liters of milk processed: ");
            liter = console.nextInt();
     
            //calculate
           cartonsNeeded = (int) (liter/CARTON);
           totalCost = (int) (liter*COST);
           totalProfit = (int) (cartonsNeeded*PROFIT);
     
           //display results
           System.out.println("You entered " +liter +" liters.");
           System.out.println("You require " +cartonsNeeded +" cartons.");
           System.out.println("Total Cost: $" +totalCost);
           System.out.println("Total Profits: $" +totalProfit);
     
        }
     
    }
    here are my outputs. (Total Profits should be somewhere around $7.0200000000000005)

    run:
    Enter liters of milk processed:
    100
    You entered 100 liters.
    You require 26 cartons.
    Total Cost: $38.0
    Total Profits: $7.0
    BUILD SUCCESSFUL (total time: 2 seconds)
    Last edited by Wtfisjava; February 9th, 2019 at 09:24 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please Help!

    what I'm doing wrong.
    Please explain. Copy and paste here any output that shows the what you are talking about. Add some comments describing what is wrong and show the desired output.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2019
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Please Help!

    My Total Profits should be $7.0200000000000005
    My output:
    run:
    Enter liters of milk processed:
    100
    You entered 100 liters.
    You require 26 cartons.
    Total Cost: $38.0
    Total Profits: $7.0
    BUILD SUCCESSFUL (total time: 2 seconds)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please Help!

    Ok, so you are saying that Total Profits should be $7.0200000000000005 instead of 7.0
    Where in the code is the variable: totalProfit assigned a value?
    Can casting to an int truncate the desired value?
    Try removing the cast to int
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Wtfisjava (February 9th, 2019)

  6. #5
    Junior Member
    Join Date
    Feb 2019
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Please Help!

    Thank you!! I changed the int to double and it worked. I knew it was a simple mistake! I've just been staring at the assignments for too long and need a break!

Tags for this Thread