使用 JSoup 拉动 HTML 后将字符串转换为整数(或其他原语)
Converting String to Integer (or other primitive) after pulling HTML with JSoup
编程新手。试图将字符串转换为整数。该字符串是使用 JSoup 从网站上检索到的。没有读过任何有帮助的东西。
下面标记为 2 和 3 的行是问题所在。我可以将这些行作为文本打印出来,但一旦我添加了 Integer.parseInt() 就不行了。我认为这个问题与摆脱白色 space 有关,但认为使用的代码可以做到这一点。
假设下面的's'是字母"A",输出结果如下(显然只有第1行在打印):
6,981,000
感谢任何帮助。
public class IncomeStatement {
String grossRevenue = "Total Revenue";
public IncomeStatement(String s) {
String incomeStatementURL = ("http://finance.yahoo.com/q/is?s="+s+"+Income+Statement&annual");
String incomeStatementTableName = "table.yfnc_tabledata1";
try {
Document doc = Jsoup.connect(incomeStatementURL).get();
Elements table = doc.select(incomeStatementTableName);
Elements row = table.select("tr");
Elements tds = row.select("td");
for (int j = 0; j < tds.size(); j++) {
if(tds.get(j).text().equals(grossRevenue)) {
/*1*/System.out.println(tds.get(j+1).text());
/*2*/System.out.println(Integer.parseInt(tds.get(j+1).text().replaceAll(",","").trim()));
/*3*/System.out.println(Integer.parseInt(tds.get(j+1).text().replaceAll(",","").replaceAll("\s+","")));
}
}
}
catch (IOException ex) {
ex.printStackTrace();
}
catch (NumberFormatException ex) {
ex.printStackTrace();
}
}
}
由于您使用 j + 1
,您应该在 j
的 for
循环测试中尽快停止一个。我建议您使用 Yoda Condition. And, you could use String.split(String)
测试 grossRevenue.equals()
以拆分 ,
(和可选的白色 space),然后使用
之类的内容迭代逗号分隔值
for (int j = 0; j < tds.size() - 1; j++) {
if (grossRevenue.equals(tds.get(j).text()) {
for (String value : tds.get(j + 1).text().split(",\s*")) {
System.out.println(Integer.parseInt(value.trim()));
}
}
}
编程新手。试图将字符串转换为整数。该字符串是使用 JSoup 从网站上检索到的。没有读过任何有帮助的东西。
下面标记为 2 和 3 的行是问题所在。我可以将这些行作为文本打印出来,但一旦我添加了 Integer.parseInt() 就不行了。我认为这个问题与摆脱白色 space 有关,但认为使用的代码可以做到这一点。
假设下面的's'是字母"A",输出结果如下(显然只有第1行在打印):
6,981,000
感谢任何帮助。
public class IncomeStatement {
String grossRevenue = "Total Revenue";
public IncomeStatement(String s) {
String incomeStatementURL = ("http://finance.yahoo.com/q/is?s="+s+"+Income+Statement&annual");
String incomeStatementTableName = "table.yfnc_tabledata1";
try {
Document doc = Jsoup.connect(incomeStatementURL).get();
Elements table = doc.select(incomeStatementTableName);
Elements row = table.select("tr");
Elements tds = row.select("td");
for (int j = 0; j < tds.size(); j++) {
if(tds.get(j).text().equals(grossRevenue)) {
/*1*/System.out.println(tds.get(j+1).text());
/*2*/System.out.println(Integer.parseInt(tds.get(j+1).text().replaceAll(",","").trim()));
/*3*/System.out.println(Integer.parseInt(tds.get(j+1).text().replaceAll(",","").replaceAll("\s+","")));
}
}
}
catch (IOException ex) {
ex.printStackTrace();
}
catch (NumberFormatException ex) {
ex.printStackTrace();
}
}
}
由于您使用 j + 1
,您应该在 j
的 for
循环测试中尽快停止一个。我建议您使用 Yoda Condition. And, you could use String.split(String)
测试 grossRevenue.equals()
以拆分 ,
(和可选的白色 space),然后使用
for (int j = 0; j < tds.size() - 1; j++) {
if (grossRevenue.equals(tds.get(j).text()) {
for (String value : tds.get(j + 1).text().split(",\s*")) {
System.out.println(Integer.parseInt(value.trim()));
}
}
}