# Thread: if statement help please!!

1. Member
Join Date
Oct 2008
Posts
39
Rep Power
0

## if statement help please!!

Can any one help with my if statement, i am trying to say if the value comm is less than or equal to 500 then comm will be 500.(I believe I have achieved that.

But want the value of comm not to change if it is over 501 and print out the last two lines, for some reason it is not doing that, any advise on what i'am doing wrong?

public class Currency{

public static void main (String[]arg){

double commRate;
String currency;
double exchange;
double amount;
double pence;
double comm=500;
double commF;

double pounds;
int payout;
double commPound;
double commPence;

int payoutPd;
int payoutPc;

System.out.println("Please enter the commission rate in percent");
commRate=UserInput.readDouble();

System.out.println("Please enter one of the following currencies you would like to exchange");
System.out.println("Euros=EUR");
System.out.println("Japanese Yen=JPY");
System.out.println("US Dollars=USD");
currency=UserInput.readString();

System.out.println("Please enter the rate of exchange to Pence");
exchange=UserInput.readDouble();

System.out.println("Please enter the amount of foreign currency to exchange");
amount=UserInput.readDouble();

pence=(int)(amount/exchange);

comm=pence*commRate/100;

commF=comm;

pounds=(pence/100);
pounds=pounds-(int)pounds;

pence=pence*100;

if(pence <=499){

System.out.println(amount+" "+currency+" goes directly to "+(int)pounds+" pounds "+pence+" pence");
System.out.println("Amount too small.");
}

else if (comm <=500){

comm=500;

commPound=comm/100;

commPence=commF/100-(int)commPound;
commPence=commPence*100;

payoutPd=(int)(pounds-commPound);
payoutPc=(int)(pence-commPence);

System.out.println(amount+" "+currency+" goes directly to "+(int)pounds+" pounds "+(int)pence+" pence commission is "+(int)commPound+" pounds "+(int)commPence+" pence.");
System.out.println("Actual sterling amounts is "+payoutPd+" pounds "+payoutPc+" pence");

}
}
}

2. Member
Join Date
Oct 2008
Posts
39
Rep Power
0
These are the three answers i should obtain if the inputs are as follows:

Input 1:
Commission rate (percentage): 1.5
Currency to convert from: JPY
Rate of exchange to Sterling pence: 1.73464
Amount to exchange: 327.55
Output 1:
327.55 JPY goes directly to 1 pounds 88 pence
Amount too small.

Input 2:
Commission rate (percentage): 2
Currency to convert from: CNY
Rate of exchange to Sterling pence: 0.1377
Amount to exchange: 327.55
Output 2:
327.55 CNY goes directly to 23 pounds 78 pence Commission is 5 pounds 0 pence
Actual sterling amount is 18 pounds 78 pence

Input 3:
Commission rate (percentage): 2.5
Currency to convert from: CNY
Rate of exchange to Sterling pence: 0.1377
Amount to exchange: 11428.15
Output 3:
11428.15 CNY goes directly to 829 pounds 93 pence Commission is 20 pounds 74 pence.
Actual sterling amount is 809 pounds 18 pence

3. ## huh?

Java Code:
`pence=(int)(amount/exchange);`
Question:
• Why are you casting a double to an int for the pence variable? amount, exchange & pence are defined as doubles.

CJSL

4. Member
Join Date
Oct 2008
Posts
39
Rep Power
0
Because for my assignment I have been asked to have amount and exchange as doubles, also after my first statement (<=500) nothing works?

5. Same question, keep everything as doubles right up until they just have to be an Int, if they in fact have to..here pence=(int)(amount/exchange); you cast pence to an int, then you do if <= 499 followed by <= 500 which will give few positive tests.

Do
Java Code:
`Double fiveHundred = new Double(500.00);`
Then wherever you need that value, do:
Java Code:
```if ( sampleValue.doubleValue() <= fiveHundred.doubleValue() )
{an action}else{other action}```
Just do all arithemtic on Doubles, going to an int only when and if needful. Use Integer.toString( resultValue.intValue()) if ints are actually needed.

Tediously work through the logic, that's the only way to get it working. There is no boilerplate for thinking through loop logic.
Last edited by Nicholas Jordan; 12-02-2008 at 02:49 PM. Reason: spelling

6. ## Please pay attetion...

My question was not why you are using doubles... my question was why are casting the result for pence as an int.
Anyway.. please put println throught out your code to debug it, so you can follow the flow of data. Once the problems have been solved, just remove the println.

Luck,
CJSL

#### Posting Permissions

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