我想从Java数组中的表中获取数据
我已经从表中解析了数据并将其存储在String中。但是的每一行都是我的String中的新行。我想将所有行放在一个数组中。
我的String x在新行中打印出每个价格:
$25,913,000
$40,388,000
$48,995,000
$3,956,000
$12,087,000
$131,339,000
$170,799,000
$41,304,000
$0
$0
$22,283,000
$0
$365,725,000
$55,888,000
$20,748,000
$40,230,000
$116,866,000
$93,735,000
$45,180,000
$2,797,000
$0
$0
$258,578,000
$40,201,000
$0
$70,400,000
$0
($3,454,000)
$107,147,000
$365,725,000
那是我的代码
try {
org.jsoup.nodes.Document doc = (org.jsoup.nodes.Document) Jsoup.connect(url_balance_year).userAgent("Mozilla/5.0").get();
Elements trs = doc.select("tr");
for (org.jsoup.nodes.Element tr : trs) {
Elements tds = tr.select(".td_genTable");
if (tds.size() == 0) continue;
org.jsoup.nodes.Element td = tds.first().siblingElements().first();
org.jsoup.nodes.Element td1 = tds.first().nextElementSibling();
org.jsoup.nodes.Element td2 = tds.first().nextElementSibling().nextElementSibling();
org.jsoup.nodes.Element td3 = tds.first().nextElementSibling().nextElementSibling().nextElementSibling();
org.jsoup.nodes.Element td4 = tds.first().nextElementSibling().nextElementSibling().nextElementSibling().nextElementSibling();
String x = td1.ownText();
System.out.println(x);
//Here I want to all the prices from String x in one Array
}
} catch (Exception e) {
}
如何从一个数组中的String x获取所有价格?
答案 0 :(得分:0)
您不仅可以使用System.out.println(x);
打印字符串,还可以使用
// declare a list first
List<String> list = new ArrayList<>();
...
list.add(x);
答案 1 :(得分:0)
替换
org.jsoup.nodes.Element td = tds.first().siblingElements().first();
org.jsoup.nodes.Element td1 = tds.first().nextElementSibling();
org.jsoup.nodes.Element td2 = tds.first().nextElementSibling().nextElementSibling();
org.jsoup.nodes.Element td3 = tds.first().nextElementSibling().nextElementSibling().nextElementSibling();
org.jsoup.nodes.Element td4 = tds.first().nextElementSibling().nextElementSibling().nextElementSibling().nextElementSibling();
String x = td1.ownText();
System.out.println(x);
与
// create a list of Strings to store texts
List<String> list = new ArrayList<String>();
// to avoid calling first() many times assign the result to a variable
Element td = tds.first();
// instead of chaining next...next...next... you can iterate over all the siblings using iterator() and while loop
Iterator<Element> iter = td.siblingElements().iterator();
while (iter.hasNext()) {
// every sibling is assigned to a new variable
Element sibling = iter.next();
// sibling text is added to list
list.add(sibling.ownText());
}
// display whole list
System.out.println(list);
// and if you really need an array you can transform it
String[] arrayWithTexts = list.toArray(new String[list.size()]);
您将丢失一些导入,但是您的IDE将帮助您完成该工作并推荐修复程序。