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

• 06-10-2010, 05:39 AM
Turismo
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.

Quote:

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

Quote:

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.
• 06-10-2010, 08:24 AM
Eranga
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
• 06-10-2010, 09:39 AM
j2me64
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.
• 06-10-2010, 03:42 PM
Turismo
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