Download JDK:
Install JDK:
Set JAVA_HOME (for Windows):
Verify Installation:
Open Command Prompt and type
java -version
javac -version
Step 2: Install an IDE (Integrated Development Environment)
IntelliJ IDEA is the most popular IDE for Java.
Download IntelliJ IDEA:
Install IntelliJ IDEA:
Configure IntelliJ:
Create a new project:
Name the project:
Create a Java Class:
Write your Java code:
Inside Main.java, write a simple program like this:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Creating a Currency Converter app in Java involves several steps. Below is a complete guide with the full code for a Currency Converter app using Java and Swing for the user interface. This example uses an API (for real-time currency conversion rates) to get exchange rates.
Set up your Java project in your preferred IDE (like IntelliJ IDEA, Eclipse, etc.).
If you're using Maven, add this dependency to pom.xml for JSON parsing:
org.json
json
20210307
Alternatively, you can manually download the JSON jar and add it to your project.
3. Java Code Implementation:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
public class CurrencyConverterApp {
// GUI Components
private JFrame frame;
private JTextField amountField;
private JComboBox fromCurrencyBox;
private JComboBox toCurrencyBox;
private JTextField resultField;
private JButton convertButton;
public static void main(String[] args) {
// Run the application
SwingUtilities.invokeLater(() -> {
try {
CurrencyConverterApp window = new CurrencyConverterApp();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
}
public CurrencyConverterApp() {
// Create the window
frame = new JFrame("Currency Converter");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
// Amount field for input
JLabel amountLabel = new JLabel("Amount:");
frame.getContentPane().add(amountLabel);
amountField = new JTextField();
amountField.setColumns(10);
frame.getContentPane().add(amountField);
// From currency dropdown
JLabel fromLabel = new JLabel("From Currency:");
frame.getContentPane().add(fromLabel);
String[] currencies = {"USD", "EUR", "GBP", "INR", "AUD", "JPY"};
fromCurrencyBox = new JComboBox<>(currencies);
frame.getContentPane().add(fromCurrencyBox);
// To currency dropdown
JLabel toLabel = new JLabel("To Currency:");
frame.getContentPane().add(toLabel);
toCurrencyBox = new JComboBox<>(currencies);
frame.getContentPane().add(toCurrencyBox);
// Result field
JLabel resultLabel = new JLabel("Converted Amount:");
frame.getContentPane().add(resultLabel);
resultField = new JTextField();
resultField.setEditable(false);
resultField.setColumns(10);
frame.getContentPane().add(resultField);
// Convert button
convertButton = new JButton("Convert");
convertButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
convertCurrency();
}
});
frame.getContentPane().add(convertButton);
}
// Fetch exchange rates and convert currency
private void convertCurrency() {
try {
String fromCurrency = fromCurrencyBox.getSelectedItem().toString();
String toCurrency = toCurrencyBox.getSelectedItem().toString();
double amount = Double.parseDouble(amountField.getText());
// API URL for real-time exchange rates (Replace API_KEY with your actual key)
String apiKey = "YOUR_API_KEY";
String urlString = String.format("https://v6.exchangerate-api.com/v6/%s/latest/%s", apiKey, fromCurrency);
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// Reading the API response
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// Parse the response
JSONObject jsonResponse = new JSONObject(response.toString());
JSONObject conversionRates = jsonResponse.getJSONObject("conversion_rates");
double rate = conversionRates.getDouble(toCurrency);
// Calculate the converted amount
double result = amount * rate;
resultField.setText(String.format("%.2f", result));
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(frame, "Error: Unable to fetch conversion rate or invalid input.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
Explanation of Code:
GUI Components:
Currency Conversion Logic:
API Request:
Error Handling:
This simple Currency Converter app demonstrates how you can use Java for GUI development and integrate real-time data from external APIs.
Looking for more job opportunities? Look no further! Our platform offers a diverse array of job listings across various industries, from technology to healthcare, marketing to finance. Whether you're a seasoned professional or just starting your career journey, you'll find exciting opportunities that match your skills and interests. Explore our platform today and take the next step towards your dream job!
Looking for insightful and engaging blogs packed with related information? Your search ends here! Dive into our collection of blogs covering a wide range of topics, from technology trends to lifestyle tips, finance advice to health hacks. Whether you're seeking expert advice, industry insights, or just some inspiration, our blog platform has something for everyone. Explore now and enrich your knowledge with our informative content!