Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java从解析的html表获取数组_Java_Arrays_Arraylist_Jsoup - Fatal编程技术网

Java从解析的html表获取数组

Java从解析的html表获取数组,java,arrays,arraylist,jsoup,Java,Arrays,Arraylist,Jsoup,我想从Java数组中的表中获取数据 我已经解析了表中的数据并将其存储在字符串中。但每一行都是我字符串中的新行。我想在一个数组中获取所有行 My 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

我想从Java数组中的表中获取数据

我已经解析了表中的数据并将其存储在字符串中。但每一行都是我字符串中的新行。我想在一个数组中获取所有行

My 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) {

    }

如何在一个数组中从字符串x获取所有价格???

而不是仅使用
System.out.println(x)打印字符串您可以使用将它们添加到列表中

// declare a list first
List<String> list = new ArrayList<>();
...
list.add(x);
//首先声明一个列表
列表=新的ArrayList();
...
增加(x);
更换

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);

//创建字符串列表以存储文本
列表=新的ArrayList();
//为了避免多次调用first(),请将结果赋给变量
元素td=tds.first();
//而不是链接下一个…下一个…下一个。。。可以使用迭代器()和while循环迭代所有同级
迭代器iter=td.siblingElements().Iterator();
while(iter.hasNext()){
//每个同级都分配给一个新变量
元素同级=iter.next();
//同级文本将添加到列表中
list.add(sibling.ownText());
}
//显示整个列表
系统输出打印项次(列表);
//如果你真的需要一个数组,你可以转换它
String[]ArrayWithText=list.toArray(新字符串[list.size()]);

您可能会错过一些导入,但您的IDE将帮助您解决这些问题并推荐修复方法。

是的,但这并不能解决我的问题,也许吧,但如果有人读了这篇文章,他应该知道这并不好,我已经尝试过了。但这会将每一行放入一个新数组中。例如,我打印出它打印的列表:[$25913000][$40388000][$48995000][$3956000],但我想把它们放在一个数组中:[$25913,00,$40388,00,$48995000]如果它是一个数组,你可以得到它的第一个元素,所以最后你会得到这个语句
list.add(x[0])
。。。嗯,如果声明为
String x
,这怎么可能是一个数组呢?不,我想要数组中的字符串你可以将列表转换为数组
String[]数组=新字符串[list.size()];列表。toArray(数组)谢谢你。如果我打印出清单,它看起来像:[现金和现金等价物,$25913000,$20289000,$20484000,$21120000][短期投资,$40388000,$53892000,$46671000,$20481000]……但我不想打印出完整的清单。例如,如何打印现金和现金等价物的第二个值?您应该真正了解数组、循环、集合和迭代器。要获取数组的第一个值,请使用:arrayWithText[0],要获取第二个值:arrayWithText[1]。。。要获取列表的第一个值,请使用:list.get(0),second:list.get(1)…如果您喜欢答案,请记住。
// 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()]);