我正在嘗試獲取我的 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.
uj5u.com熱心網友回復:
所以你的代碼中發生了很多事情。我建議你重構你的程式,它有很多重復。
關于所需的庫:
匯入 JSONObject 可能不起作用,因為您沒有在依賴項中列出它。
對于 Maven,請查看:https
://mvnrepository.com/artifact/org.json/json
如果您不使用依賴項管理工具,請從此處下載 jar:https ://mvnrepository.com/artifact/org.json/ JSON
`import java.net.URLConnection;` 也沒有用,
我使用`import java.net.HttpURLConnection; import java.net.URL;` 并且有效。
uj5u.com熱心網友回復:
不同的服務器生成略有不同的 JSON xml 檔案。您需要構建您的應用程式以根據模板的布局方式決議 JSON 檔案。你只需要這樣做一次。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/535237.html
上一篇:如何讓彈性專案跳到下一行?
