【发布时间】:2022-11-17 03:14:01
【问题描述】:
我正在尝试获取我的 java 兑换货币程序的实时汇率。我看到这可以通过使用来自互联网的 API 并在 java 程序中导入网站 URL 来获得实时汇率来完成。但是,我在使用 JSON 时遇到了问题,并收到了一些阻止我运行该程序的错误。我不确定要导入什么来修复错误。我很新,我不确定这是否应该很困难,或者我在这里做错了什么。先感谢您。
`
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();
}
}
`
我试过导入
导入 org.json.JSONObject;
和
导入 java.net.URLConnection;
but this doesn't fix the error.
【问题讨论】:
-
显示错误没有帮助吗?另外,您使用的是哪种依赖管理工具?你有没有把 JSON 库放在你的依赖项中?
标签: java api currency converters