Results 1 to 4 of 4
  1. #1
    Turismo is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default problem figuring out how to carry over and drop values in currency.

    I'm in a beginner JAVA class and we're only 5 weeks in. We are using jGrasp to write our code. We haven't gotten into anything particularly fancy, but we have a doozy of an assignment to which we can only use what we have learned so far.

    We had a similar problem that asked us to input seconds, and then covert that to DD:HH:MM:SS and I was able to figure that one out, but...

    This problem is dealing with converting old British currency to the new decimal system, and then changing it to dollars and then Euros. I get the basic concept, and my code will return basically the right numbers, but the pounds value has a decimal equal to the pence number, and the pence value is also in decimal form. I would like to remove the decimal in pounds value and the pence value. Until my recent compile, I was getting values over 100 for Pence, which is completely wrong, but as an amateur I could not tell you why it stopped doing that.

    Am I missing something in if statements?

    Here is my code so far (before I enter in the dollar and Euro values). We have not covered BigDecimal or any currency classes. Also, I am pretty sure the conversions we were given aren't 100% accurate, but are for demonstration purposes.



    import javax.swing.*;

    public class OldPounds
    {
    public static void main (String [] args)
    {
    String oldPoundInput;
    String oldShillingInput;
    String oldPenceInput;
    int oldPound;
    int oldShilling;
    int oldPence;

    //Input old Pounds
    oldPoundInput = JOptionPane.showInputDialog( "Enter Old Pounds amount.");
    //Input old Shillings
    oldShillingInput = JOptionPane.showInputDialog( "Enter Old Shillings amount.");
    //Input old Pence
    oldPenceInput = JOptionPane.showInputDialog( "Enter Old Pence amount.");

    //Convert Pounds from String to int
    oldPound = Integer.parseInt(oldPoundInput);
    //Convert Shillings from String to int
    oldShilling = Integer.parseInt(oldShillingInput);
    //Convert Pence from String to int
    oldPence = Integer.parseInt(oldPenceInput);


    int newPence; //To convert from shillings to it's current pence value
    newPence = .20*(oldShilling);

    int halfPence; //to convert from the old pence system to the current value
    halfPence = 2*(oldPence);

    int newPenceTotal; //to combine both previous pence calculations into one workable pence value
    newPenceTotal = (newPence + halfPence);



    int penceToPound; //How many Pence in a pound
    penceToPound = (newPenceTotal/100);

    int penceToPoundRemainder; //remainder
    penceToPoundRemainder = (newPenceTotal%100);

    newPenceTotal = penceToPoundRemainder; //changing newPenceTotal to the remainder

    int poundToPound; //converting the old pound system to the new pound. ratio of 1:1
    poundToPound = oldPound;

    int poundTotal;
    poundTotal = (poundToPound+penceToPound); //adding both ways of converting to pounds



    JOptionPane.showMessageDialog( null, poundTotal + " Pounds, "
    + newPenceTotal + " Pence.");

    System.exit(0);
    }
    }
    So if I enter in 12 Pounds, 200 Shillings, 100 Pence, I get 14.4 Pounds, 40.0 Pence.

    I also get a minor error saying that

    OldPounds.java:37: possible loss of precision
    found : double
    required: int
    newPence = .20*(oldShilling);
    Which I get, because it's an int returning a decimal value, but the other primitives seem like over-kill.

    I know I've rambled on a lot here, but any suggestions would be greatly appreciated. I'm not necessarily looking for somebody to code this for me, but rather some advice and instruction on the proper method of doing so.

    Thank you very much.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I didn't look at the code completely. Once looking at your title, I believe the following will gives you a better understanding.

    http://www.java-forums.org/java-anno...8-03-29-a.html

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    in java, if you assign a value with a smaller number type to a value with a bigger number type the compiler will cast the value for you. but if you try the contrary like in your example, the compiler will not cast the double to an int because of the loss of precision. but, the compiler allows you to cast the value assuming you know what you are doing. so this statement will work in your code

    newPence = (int)(.20 * (oldShilling));

    hope you understand. you will find more details here. have fun.

  4. #4
    Turismo is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default

    j2me64,

    Thank you very much. That trick worked perfectly. It's not something we have covered yet, so I think I will talk to the professor about possibly covering that for his future students before this assignment. It may have been an oversight for this semester, although it's not in our book at this point.

    Maybe it's a test to see if we're resourceful or not? lol

    Your help is much appreciated.

Similar Threads

  1. drag and drop - disable intra component drop
    By tomba in forum AWT / Swing
    Replies: 4
    Last Post: 12-09-2009, 02:01 PM
  2. Replies: 4
    Last Post: 10-20-2009, 12:16 AM
  3. Need Support for Using Network service providers channel to carry speech
    By Nagasubramanian in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 02-13-2009, 01:10 PM
  4. Replies: 6
    Last Post: 01-21-2009, 03:56 AM
  5. figuring out the current colour of my appelet
    By vortexprogramming in forum New To Java
    Replies: 2
    Last Post: 07-15-2008, 05:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •