gpt4 book ai didi

Java - 线程 "AWT-EventQueue-0"java.lang.NumberFormatException : create a URL from User Input using JComboBox 中的异常

转载 作者:行者123 更新时间:2023-11-29 05:13:06 25 4
gpt4 key购买 nike

我正在构建一个 GUI 来检索历史天气数据。我有 2 个 JFrame,第一个框架要求用户输入他/她想要检索给定天气数据的日期。

单击“确定”按钮后,应将数据传递给 urlString,并应从该 URL 检索天气数据并显示在第二帧上。

frame1 中的代码:

okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String s = event.getActionCommand();
if (s.equals("OK")) {
String a = (String) airportBox.getSelectedItem();
String d = (String) dayBox.getSelectedItem();
String m = (String) monthBox.getSelectedItem();
String y = (String) yearBox.getSelectedItem();
String urlString = frame2.makeURL(a, y, m, d);
frame2.openURL(urlString);
frame2.setVisible(true);
}
}
});

frame2 中的代码:

public void openURL(String urlString) {
try {
// Open connection
URL u = new URL(urlString);
URLConnection connection = u.openConnection();

// check to make sure the page exists
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int code = httpConnection.getResponseCode();
String message = httpConnection.getResponseMessage();
System.out.println(code + " " + message);
if (code != HttpURLConnection.HTTP_OK)
return;

// Read server response
InputStream instream = connection.getInputStream();
Scanner in = new Scanner(instream);
//use comma to separate values
in.useDelimiter(",");

/*
* Store Weather Data in ArrayList
*/
// display server response to console
WeatherData data = new WeatherData();
ArrayList<String> weatherData = data.getWeatherData();
while (in.hasNext()) {
String input = in.next();
weatherData.add(input);
}

int averageTemperature = data.calculateAverageTemperature(weatherData);
Container contentPane = this.getContentPane();

//add Label at the Top
JPanel centerPanel = new JPanel(new GridLayout(5, 2));
contentPane.add(graphImage, BorderLayout.CENTER);

JLabel temper = new JLabel("Average Temperature: ");
JTextField tempField= new JTextField(String.valueOf(averageTemperature));
tempField.setEditable(false);

centerPanel.add(temper);
centerPanel.add(tempField);
contentPane.add(centerPanel, BorderLayout.SOUTH);
}
catch (IOException i) {
System.out.println("Can't display page " + urlString);
}
}

private JPanel centerPanel;

public String makeURL(String a, String y, String m, String d) {
String urlString = "http://www.wunderground.com/history/airport/"+a+"/"+y+"/"+m+"/"+d+"/DailyHistory.html?HideSpecis=%201&format=1";
return urlString;
}

openURL() 方法似乎会导致以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "temperature history"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1250)
at java.lang.Double.parseDouble(Double.java:540)
at WeatherData.calculateAverageTemperature(WeatherDataTest2.java:171)
at WeatherGraphFrame.openURL(WeatherGUITest.java:227)
at WeatherGUIFrame$1.actionPerformed(WeatherGUITest.java:133)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

我希望您了解基本概念。如果您需要更多信息,请告诉我。

最佳答案

如以上评论所述,您正在读取来自 HTTP 请求的输入并期望一个或多个数值。相反,该请求返回一个文本字符串“temperature history”。我查看了该站点,认为您的方法存在缺陷。您提交的 URL 返回 HTML 页面。这些页面包含您要查找的天气历史记录 - 但它们还包含用于在浏览器中格式化此数据的 HTML。

众所周知,从 HTML 中提取数据是一种不好的做法。问题是您无法控制 HTML 的结构。该站点可以随时更改它。解决方案是调用一个纯 API(如果存在)。在这种情况下,该站点似乎确实支持 REST API。请参阅:http://www.wunderground.com/weather/api/有关如何执行此操作的详细信息。

因此我建议您更改代码以调用 REST API。因此,您的程序将以有保证的已知格式接收数据。看起来您可以选择接收 XML 或 JSON。

关于Java - 线程 "AWT-EventQueue-0"java.lang.NumberFormatException : create a URL from User Input using JComboBox 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27591662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com