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 3 of 3

Thread: if-else statement: how do I get the program I'm writing to decide which math equation to use?

  1. #1
    Junior Member codingninja's Avatar
    Join Date
    Aug 2014
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default if-else statement: how do I get the program I'm writing to decide which math equation to use?

    Hi everyone. I'm currently studying for a midterm I have coming up in my intro to Java course. The prof gave us practice coding questions so we can brush up on what it is we will need to know. I am stuck and I'm hoping someone here can help. I have a feeling it's an easy fix, but I can't see it..

    ******I am NOT asking you to do my homework for me! This is not a homework assignment. It's simply practice sheets my prof gave us to prepare for the practical side of our exam. So I'm not looking for you to do it for me. There are only TWO spots I am stuck on. ********

    This is what we are required to do:

    Write a program called CurrencyExchange that will either convert Canadian money to U.S.
    funds or convert U.S. funds to Canadian.
    This program will ask the user to enter the following three values:

    1) The current exchange rate in Canadian dollars (e.g. $1.1151 Canadian dollars per US
    dollar)

    2) A single character to indicate which kind of currency to convert from (‘C’ for Canadian or
    ‘U’ for U.S.)

    3) The amount of money in the specified currency to be converted (e.g. $235.50)


    The program will then convert the amount of money to the other currency as follows:

    - If the starting currency is in Canadian funds, divide the amount by the exchange rate
    - If the starting currency is in U.S. funds, multiply the amount by the exchange rate

    Next the program will ROUND the converted funds to TWO DECIMAL PLACES.

    Finally, the program will report the amount of money before and after the conversion as shown
    in the sample output below.

    Anyways, without further ado, here is my code thus far:

    /**
     * Program Name: CurrencyExchange.java
     * 
     * Purpose: Write a program that will either convert Canadian money to U.S. funds or convert U.S. funds to Canadian.
     * 
     * Coder: Bronwen
     * Date: Oct 5, 2014
     */
     
    import java.util.Scanner;
     
    public class CurrencyExchange 
    {
     
    	public static void main(String[] args) 
    	{
    		// Scanner.
    		Scanner input = new Scanner(System.in);
     
    		// Get user input: current exchange rate.
    		System.out.println("Please enter the current exchange rate in Canadian dollars, and press ENTER.");
     
    		double exchangeRate = input.nextDouble();
     
    		System.out.println("Thank you. Now, which currency would you like to convert from?");
     
    		// Flush the buffer.
    		input.nextLine();
     
    		// Get user input: Canadian or US?
    		System.out.println("Please press C for Canadian, or U for US.");
     
    		String letter = input.nextLine();
     
    		// Get user input: Amount to be converted?
    		System.out.println("Thank you. Finally, please enter the total value which you would like converted.");
     
    		double toConvert = input.nextDouble();
     
    		double endResult;
     
    		// Process: use the if-else statement.
    		if (letter == "C")
    			endResult = toConvert / exchangeRate //<-- it's asking me to put a ; but when I do, it ALWAYS converts
    												 //	   resorts to using THIS calculation. HELP!
    		else
    			endResult = toConvert * exchangeRate;
     
    		// Process: Round the converted funds to TWO decimal places.
    		final double ROUNDED_CONVERSION = 0.005; // .005 rounds to TWO decimal places.
     
    		endResult += ROUNDED_CONVERSION;
    		endResult *= 100; // 100 for TWO decimal places.
     
    		// Cast to an int to CUT OFF the excess decimals. Use a temporary variable called tempVar.
    		int tempVar = (int)endResult;
     
    		// Divide tempVar by 100 to move decimal TWO places to left. Assign result back to endResult.
    		endResult = (double)tempVar/100; // Casting to a double to avoid the INTEGER SHARK.
     
    		// Output. 
    		System.out.println("You can convert $" + toConvert + " to $" + endResult); //<-- Here, I'm having trouble getting
    															      //    the program to print out the words
    															      //    "Canadian" and "US" in the right spots
     
    	}//end main
    }//end class

    Problem 1:
    I'm having trouble with the if-else part of this example. I have tried numerous ways of writing the statement so that it correctly decides which block of code to execute first. The problem is, I keep getting syntax errors.

    Problem 2:
    How do I get the program to output the last statement correctly so that it reads either "You can exchange $235.5 US for $262.61 Canadian." OR "You can exchange $262.61 Canadian for $235.5 US." depending on which conversion you have asked the program to do.

    Thank you so much in advance for your help on this one!
    As I said, this is an intro course. We are only one month in and this is the most programming I've done in my life. So feel free to explain to me in laymen terms how you'd go about writing the correct code. Pretend I'm stupid. Cause right now, I feel it.


  2. #2
    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: if-else statement: how do I get the program I'm writing to decide which math equation to use?

    Problem 1: Do not use '==' to compare String objects. Use the equals() method instead. You might also consider using a char as the menu choice rather than a String. With a char, the '==' would be fine.

    Problem 2: With an if() statement.

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

    codingninja (October 6th, 2014)

  4. #3
    Junior Member codingninja's Avatar
    Join Date
    Aug 2014
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thank you Greg, I really appreciate your help!

Similar Threads

  1. i need help writing a code for a math problem
    By djjava in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 6th, 2013, 01:13 PM
  2. Does there exist a Jar that solves Math Equation?
    By williamshen25 in forum Java Theory & Questions
    Replies: 3
    Last Post: June 29th, 2012, 11:45 AM
  3. Help writing the IF statement for Y/N
    By limeleaf in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 9th, 2012, 06:50 AM
  4. If-Else Statement and Math
    By mikecancelosi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2012, 01:31 AM
  5. Replies: 1
    Last Post: November 6th, 2011, 09:48 PM