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: First timer here! Help with very very basic problem

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

    Exclamation First timer here! Help with very very basic problem

    Hello my name is Brandon and I am taking intro to Java for the first time and I do not have and previous experience with programming.

    I am stuck on a HW problem:

    1) Sales Prediction - The East Coast sales division of a company generates 62 percent of total sales. Based on that percentage, write a program that will predict how much the East Coast division will generate if the company has $4.6 million in sales this year.

    The basic program I wrote seems to not be working and could use some help.

    public class SalesPrediction
    {
    public static void main(String[] args)
    {
    int x = 4600000;
    int y = 0.62;

    int total = x * y;

    System.out.println("The East Coast will generate $ " + total + " this year. ");
    }
    }

    The error I am getting says possible loss of precision.

    Again I am a complete beginner so any help would be great! Thanks in advance


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: First timer here! Help with very very basic problem

    int y = 0.62;
    Is not valid. An integer (int) cannot contain a decimal number. If you want to use a decimal number, you need to use a double
    double y = 0.62;

    Also, your int total needs to be a double total, or the fractional part of the x * y equation will be lost.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    brobertson300 (January 31st, 2014)

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

    Default Re: First timer here! Help with very very basic problem

    Thank you so much! Do you think this program is valid; would you have done it another way? Im trying to learn as much as possible and just pick peoples brain.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: First timer here! Help with very very basic problem

    Welcome to the Forum! Please read this topic to learn how to post your code correctly along with other useful info for newcomers.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    brobertson300 (January 31st, 2014)

  7. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: First timer here! Help with very very basic problem

    For your purposes, it seems fine. If you wanted to "go the extra mile" and pretty up the output a bit, you could format the total appropriately before you print it. By that, I mean you could make it so only the first two decimal places are displayed (since that is usually how you would see money displayed). You can do that quite easily with the NumberFormat class.
    Here is a brief example of how it works:
    double money = 100.25789;
    NumberFormat formatter = NumberFormat.getCurrencyInstance();
    String moneyString = formatter.format(money);
    System.out.println("The money amount is "+moneyString); // This will print out: The money amount is $100.26

    Another common practice, is not using double for money. Since you are probably not dealing with "real" money, it isn't an issue. But in real-world applications where money precision is important (like an application for a Bank), the BigDecimal class is recommended by most people. This is because there can be small mathematical mistakes with doubles due to something called "floating-point arithmetic" and because the computer cannot always accurately store a base 10 decimal number as bits. An example of this error can be seen if you run this basic code:
    double number = 0.1 * 0.1;
    System.out.println(number);
    We know that the answer to that should be 0.01, but due to how the computer stores the number "0.1", the computer will give the result: 0.010000000000000002
    It's just a fun little fact you might see on an exam or something some day, lol.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. The Following User Says Thank You to aussiemcgr For This Useful Post:

    brobertson300 (January 31st, 2014)

Similar Threads

  1. [SOLVED] Basic Java Problem
    By digitalsystems in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 29th, 2013, 12:49 PM
  2. Problem with timer
    By Hikaros in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 17th, 2013, 12:30 AM
  3. How do I create a most basic java timer ?
    By steven_bishop in forum Java Theory & Questions
    Replies: 3
    Last Post: December 5th, 2012, 01:53 AM
  4. Thread/timer problem
    By korbal in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2010, 05:59 PM
  5. bubble sort timer problem
    By JavaNoob82 in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 12th, 2010, 09:22 AM