Results 1 to 6 of 6
  1. #1
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Default 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. #2
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Default

    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. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Default

    Because for my assignment I have been asked to have amount and exchange as doubles, also after my first statement (<=500) nothing works?

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Default

    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 01:49 PM. Reason: spelling
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. If statement, please help??
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 11-23-2008, 02:58 PM
  2. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 05:16 AM
  3. Help with if statement
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:53 AM
  4. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM
  5. If Statement
    By aDrizzle in forum New To Java
    Replies: 4
    Last Post: 07-08-2007, 08:55 PM

Posting Permissions

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