DEV Community

Mohammed mhanna
Mohammed mhanna

Posted on

πŸ’± Build a Simple Currency Converter in Java

Currency converters are everywhere β€” in banks, travel apps, and e-commerce websites. Let’s build a basic console-based version in Java to understand the core logic.

This project will teach you:
βœ” Handling user input with Scanner
βœ” Working with decision-making (if-else)
βœ” Doing simple math with exchange rates
βœ” Formatting output with printf


πŸ“ 1. The Idea

We want a program that:

Lets the user pick a conversion type (e.g., USD β†’ EUR).

Asks how much money they want to convert.

Applies a fixed exchange rate (for simplicity).

Prints the converted amount.

⚠️ Note: This program uses hardcoded exchange rates. In a real-world app, you’d fetch live rates from an API.


πŸ’» 2. The Java Program

import java.util.Scanner;

public class CurrencyConverter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("=== Currency Converter ===");
        System.out.println("Supported conversions:");
        System.out.println("1. USD to EUR");
        System.out.println("2. EUR to USD");
        System.out.println("3. USD to GBP");

        System.out.print("Enter your choice (1-3): ");
        int conversionChoice = scanner.nextInt();

        System.out.print("Enter the amount you want to convert: ");
        double amountToConvert = scanner.nextDouble();

        double convertedAmount = 0.0;

        // Fixed exchange rates
        double usdToEurRate = 0.92;
        double eurToUsdRate = 1.09;
        double usdToGbpRate = 0.78;

        if (conversionChoice == 1) {
            convertedAmount = amountToConvert * usdToEurRate;
            System.out.printf("%.2f USD = %.2f EUR%n", amountToConvert, convertedAmount);
        } else if (conversionChoice == 2) {
            convertedAmount = amountToConvert * eurToUsdRate;
            System.out.printf("%.2f EUR = %.2f USD%n", amountToConvert, convertedAmount);
        } else if (conversionChoice == 3) {
            convertedAmount = amountToConvert * usdToGbpRate;
            System.out.printf("%.2f USD = %.2f GBP%n", amountToConvert, convertedAmount);
        } else {
            System.out.println("Invalid choice. Please restart and choose between 1-3.");
        }

        scanner.close();
    }
}

Enter fullscreen mode Exit fullscreen mode

πŸ” 3. Key Parts Explained
βœ… User Input

We use Scanner to read choices and amounts:

int conversionChoice = scanner.nextInt();
double amountToConvert = scanner.nextDouble();

Enter fullscreen mode Exit fullscreen mode

βœ… Hardcoded Exchange Rates

Instead of live rates, we set fixed values:

double usdToEurRate = 0.92;
double eurToUsdRate = 1.09;
double usdToGbpRate = 0.78;
Enter fullscreen mode Exit fullscreen mode

βœ… Decision-Making with if-else

if (conversionChoice == 1) {
    convertedAmount = amountToConvert * usdToEurRate;
    System.out.printf("%.2f USD = %.2f EUR%n", amountToConvert, convertedAmount);
}
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ The program branches depending on the user’s choice.


βœ… Formatting Output

We use System.out.printf with %.2f to round values to 2 decimals:

System.out.printf("%.2f USD = %.2f EUR%n", amountToConvert, convertedAmount);

Enter fullscreen mode Exit fullscreen mode

πŸš€ 4. Example Run:

=== Currency Converter ===
Supported conversions:
1. USD to EUR
2. EUR to USD
3. USD to GBP
Enter your choice (1-3): 1
Enter the amount you want to convert: 100

100.00 USD = 92.00 EUR

Enter fullscreen mode Exit fullscreen mode

πŸ’‘ 5. Improvements

Want to make it better? Try adding:

More currency options (JPY, INR, etc.).

Looping until the user chooses to exit.

Input validation (handle wrong values).

Live rates via an API like ExchangeRate API
.


🎯 Final Thoughts

This project is a great exercise for beginners to practice:
βœ” User interaction
βœ” Simple math
βœ” Conditional logic
βœ” Output formatting

It’s the foundation for building more advanced apps β€” like a real-time currency converter with an API.


πŸ‘‰ What do you think β€” should we extend this project to support more currencies and live API rates in the next post?

Top comments (0)