Java.lang.NumberFormatException error: empty string- Exchange Rate application(Java.lang.NumberFormatException 错误:空字符串 - 汇率应用程序)
问题描述
我正在编写一个程序,它从 http://www 获取每日汇率.tcmb.gov.tr/kurlar/today.xml 并将它们保存到列表中.这是我的代码:
I'm writing a program which gets daily exchange rates from http://www.tcmb.gov.tr/kurlar/today.xml and saves them to the List. Here is my code:
public class ParseTheXml {
private List<CurrencyPojo> currencyList;
public void setCurrencyList(List<CurrencyPojo> currencyList) {
this.currencyList = currencyList;
}
public List<CurrencyPojo> getCurrencyList() throws ParserConfigurationException {
currencyList = new ArrayList<CurrencyPojo>();
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URL url = new URL("http://www.tcmb.gov.tr/kurlar/today.xml");
Document document = builder.parse(url.openStream());
NodeList nodeList = document.getElementsByTagName("Currency");
for (int i = 0; i < nodeList.getLength(); i++) { // döngü her currency türü için
Element element = (Element) nodeList.item(i);
CurrencyPojo currencyPojoObject = new CurrencyPojo();
currencyPojoObject.setCrossOrder(Integer.parseInt(element.getAttribute("CrossOrder")));
currencyPojoObject.setKod(element.getAttribute("Kod"));
currencyPojoObject.setCurrenyCode(element.getAttribute("CurrencyCode"));
currencyPojoObject.setUnit(Integer.parseInt(element.getElementsByTagName("Unit").item(0).getTextContent()));
currencyPojoObject.setIsim(element.getElementsByTagName("Isim").item(0).getTextContent());
currencyPojoObject.setCurrencyName(element.getElementsByTagName("CurrencyName").item(0).getTextContent());
currencyPojoObject.setForexBuying(Double.parseDouble(element.getElementsByTagName("ForexBuying").item(0).getTextContent()));
currencyPojoObject.setForexSelling(Double.parseDouble(element.getElementsByTagName("ForexSelling").item(0).getTextContent())); // Last 3 codes have Emppty String errors
currencyPojoObject.setBanknoteBuying(Double.parseDouble(element.getElementsByTagName("BanknoteBuying").item(0).getTextContent()));
currencyPojoObject.setBanknoteSelling(Double.parseDouble(element.getElementsByTagName("BanknoteSelling").item(0).getTextContent()));
currencyList.add(currencyPojoObject);
}
} catch (Exception e) {
e.printStackTrace();
}
return currencyList;
}
在获得 ForexSelling、BanknoteBuying 和 Banknote Selling 值的部分,我得到了
On the part that getting ForexSelling, BanknoteBuying and Banknote Selling values,I get
java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at javaxmlparseandshow.ParseTheXml.getCurrencyList(ParseTheXml.java:55)
at javaxmlparseandshow.ParseTheXml.main(ParseTheXml.java:75)
有人可以帮我解决这些问题吗?提前致谢.
Can someone help me to fix these? Thanks in advance.
推荐答案
或者你可以设置0为空的xml字符串
Alternatively you can set 0 to empty xml strings
private double parseDouble(String val){
if(val== null || val.isEmpty())
return 0.0;
else
return Double.parseDouble(val);
}
前:
currencyPojoObject.setForexSelling(parseDouble(element.getElementsByTagName("ForexSelling").item(0).getTextContent()));
这篇关于Java.lang.NumberFormatException 错误:空字符串 - 汇率应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java.lang.NumberFormatException 错误:空字符串 - 汇率应


基础教程推荐
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01