首页 > 解决方案 > 如何通过关键字从字符串数组中删除对象,或在数组长度上搜索特定对象?

问题描述

感谢耐心,因为我还在学习。我正在构建一个 Web 解析应用程序,它允许最终用户输入任何网址并提取源 html,然后将其保存到文本文件中。我现在正试图将该应用程序专门用于从 CNBC 市场提取股票数据。我有一个名为 name 的扫描仪,它会提取用户正在搜索的股票的名称。然后,我需要能够在用户输入的字符串的数组长度上迭代一个 for 循环。当我这样做时,我得到 .length 无法解决为错误。我可以使用的最佳替代方案是什么?Java 似乎没有像我使用过的大多数语言那样简单的长度函数。

public class WikiScraper {

    public static void main(String[] args) {
        Scanner address = new Scanner (System.in);
        System.out.println("Enter the complete url (including http://) of the site you would like to parse:");
        String html = address.nextLine();
        try {
            Document doc = Jsoup.connect(html).get();
            System.out.printf("Title: %s", doc.title());
            //Try to print site content
            System.out.println("");
            System.out.println("Writing html contents to 'html.txt'...");
            //Save html contents to text file
            PrintWriter outputfile = new PrintWriter("html.txt");
            outputfile.print(doc.outerHtml());
            outputfile.close();

            //Select stock data you want to retrieve
            Scanner stock = new Scanner (System.in);
            System.out.println("Enter the name of the stock you want to check");
            String name = stock.nextLine();

            //Pull data from CNBC Markets
            for(Element money : doc.select("tbody")) {
                final String market = money.select("tbody").text();
                final String index = money.select("td").text();
                String [] funds = index.split(" ");
                //System.out.println(Arrays.toString(funds));
                Pattern pattern = Pattern.compile(" ");
                funds = pattern.split(index);
                for (var i = 0; i < market.length; i++)
                    if (market == name) {
                        System.out.println(market);
                        break;
                    }
                System.out.println(market);
                //System.out.println(index);
            }
        } catch (IOException e) {
        e.printStackTrace();
        }
    }
}

标签: javahtmlweb-scrapingstring-length

解决方案


推荐阅读