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();
}
}
π 3. Key Parts Explained
β
User Input
We use Scanner to read choices and amounts:
int conversionChoice = scanner.nextInt();
double amountToConvert = scanner.nextDouble();
β Hardcoded Exchange Rates
Instead of live rates, we set fixed values:
double usdToEurRate = 0.92;
double eurToUsdRate = 1.09;
double usdToGbpRate = 0.78;
β
Decision-Making with if-else
if (conversionChoice == 1) {
convertedAmount = amountToConvert * usdToEurRate;
System.out.printf("%.2f USD = %.2f EUR%n", amountToConvert, convertedAmount);
}
π 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);
π 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
π‘ 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)