I am trying to get live exchange rates for my java exchange currency program. I saw that this could be done using API from the internet and import the website URL in the java program to get live exchange rates. However I am having trouble working with JSON and getting a few more errors that prevent me from running the program. I am not sure what to import in order to fix the errors. I am quite new and I am not sure if this should be difficult or am I doing something wrong here. Thank you in advance.
`
package currencyConverterGUI;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat; // import for decimal place limitation
import java.net.URL;
import org.json.JSONObject;
import java.io.*;
import java.net.URLConnection;
public class currencyGUI extends JFrame //inherit from JFrame
{
private static final DecimalFormat df = new DecimalFormat("0.00000"); // use DecimalFormat to round double numbers to 5 decimal places
private JButton btnConvert; // generated by GUI designer
private JPanel JPanelMain; // generated by GUI designer
private JTextField textAmount; // generated by GUI designer
private JComboBox textFrom; // generated by GUI designer
private JComboBox textTo; // generated by GUI designer
private JLabel result; // generated by GUI designer
public currencyGUI() {
btnConvert.addActionListener(new ActionListener() { // button reacts to user click; generated by GUI designer
@Override
public void actionPerformed(ActionEvent e)
{
double total;
double amount = Double.parseDouble(textAmount.getText()); // check if input amount is a number and read the input if it is a number
int index = textTo.getSelectedIndex(); //get index of selected currency from the first combo box
if(textFrom.getSelectedItem() == "USD") // if USD is selected in the first combo box, then switch for each currency
{
switch (index) {
case 0:
total = amount * 1;
result.setText(df.format(total) " USD");
break;
case 1:
total = amount * 0.86;
result.setText(df.format(total) " EUR");
break;
case 2:
total = amount * 1.88;
result.setText(df.format(total) " BGN");
break;
case 3:
total = amount * 0.000060;
result.setText(df.format(total) " BTC");
break;
case 4:
total = amount * 2.98;
result.setText(df.format(total) " ADA");
break;
}
}
if(textFrom.getSelectedItem() == "EUR") // if EUR is selected in the first combo box, then switch for each currency
{
switch (index) {
case 0:
total = amount * 1.04;
result.setText(df.format(total) " USD");
break;
case 1:
total = amount * 0.1;
result.setText(df.format(total) " EUR");
break;
case 2:
total = amount * 1.95;
result.setText(df.format(total) " BGN");
break;
case 3:
total = amount * 0.000063;
result.setText(df.format(total) " BTC");
break;
case 4:
total = amount * 3.18;
result.setText(df.format(total) " ADA");
break;
}
}
if(textFrom.getSelectedItem() == "BGN") // if BGN is selected in the first combo box, then switch for each currency
{
switch (index) {
case 0:
total = amount * 0.53;
result.setText(df.format(total) " USD");
break;
case 1:
total = amount * 0.51;
result.setText(df.format(total) " EUR");
break;
case 2:
total = amount * 1;
result.setText(df.format(total) " BGN");
break;
case 3:
total = amount * 0.000032;
result.setText(df.format(total) " BTC");
break;
case 4:
total = amount * 1.63;
result.setText(df.format(total) " ADA");
break;
}
}
if(textFrom.getSelectedItem() == "BTC") // if BTC is selected in the first combo box, then switch for each currency
{
switch (index) {
case 0:
total = amount * 16446.8;
result.setText(df.format(total) " USD");
break;
case 1:
total = amount * 15851.4;
result.setText(df.format(total) " EUR");
break;
case 2:
total = amount * 31043.1;
result.setText(df.format(total) " BGN");
break;
case 3:
total = amount * 1;
result.setText(df.format(total) " BTC");
break;
case 4:
total = amount * 50467.4;
result.setText(df.format(total) " ADA");
break;
}
}
if(textFrom.getSelectedItem() == "ADA") // if ADA is selected in the first combo box, then switch for each currency
{
switch (index) {
case 0:
total = amount * 0.33;
result.setText(df.format(total) " USD");
break;
case 1:
total = amount * 0.32;
result.setText(df.format(total) " EUR");
break;
case 2:
total = amount * 0.62;
result.setText(df.format(total) " BGN");
break;
case 3:
total = amount * 0.000020;
result.setText(df.format(total) " BTC");
break;
case 4:
total = amount * 1;
result.setText(df.format(total) " ADA");
break;
}
}
}
});
}
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("Currency Converter");
frame.setContentPane(new currencyGUI().JPanelMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true); // make pane visible
URL url = new URL("https://api.exchangeratesapi.io/latest?symbols=USD,GBP");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String jsonText = readAll(in);
JSONObject yourData = new JSONObject(jsonText);
}
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
}
`
I have tried importing
import org.json.JSONObject;
and
import java.net.URLConnection;
but this doesn't fix the error.
CodePudding user response:
So there is a lot going on in your code. I recommend you to refactor your program, it has a lot of repetitions.
Regarding the required libraries:
importing JSONObject probably does not work because you haven't listed it in your dependencies.
For maven check out: https://mvnrepository.com/artifact/org.json/json
If you're not using a dependency management tool download the jar from here: https://mvnrepository.com/artifact/org.json/json
There is also no use for `import java.net.URLConnection;`
I used `import java.net.HttpURLConnection; import java.net.URL;` and it worked.
CodePudding user response:
Different servers produce slightly differnet JSON xml files. You need to construct your app to parse the JSON file accodring to how the template was laid out. You'd only need to do this once.