Jsoup 和 for 循环 nosuchelementexception

问题描述 投票:0回答:0

我正在尝试通过不断更改 url 字符串来收集有关雅虎金融的数据,这是我的代码以使其更有意义:

public static void gatherLoop() throws NoSuchElementException, IOException{
        for(var i = 1; i<99; i++){
        BufferedReader gatherStocks = new BufferedReader(new FileReader("stockNames.txt"));
        Stream<String> load = Files.lines(Paths.get("stockNames.txt"));
        String loadString = load.skip(i).findFirst().get();
        String glURL = "http://finance.yahoo.com/quote/"+loadString+"?p="+loadString+"&.tsrc=fin-srch";

        if(glURL.equals("http://finance.yahoo.com/quote/?p=&.tsrc=fin-srch")){
            gatherStocks.close();
            System.out.println("Loaded Stocks");
            System.out.println("If Their Is No Stocks Popping Up, You Have No Saved Stocks");            
        }else{
            Document doc = Jsoup.connect(glURL).get();
            Elements contentA = doc.getElementsByClass("D(ib) Fz(18px)");
            System.out.println("|------------------------------------------------|");
            for(Element a: contentA){
            System.out.println("| "+i+". Name: "+a.text());
            gatherStocks.readLine();
            }
        }
    }

虽然当我运行代码时,我在编译器中遇到运行时错误:

Exception in thread "main" java.util.NoSuchElementException: No value present
        at java.base/java.util.Optional.get(Optional.java:143)
        at StockAnalysis.saves.gatherLoop(saves.java:32)
        at StockAnalysis.saves.gatherSavedStocks(saves.java:25)
        at StockAnalysis.Home.main(Home.java:27)

我尝试的第一件事就是执行 i++ 并再次运行 void 但这会将 i 重置为零。然后我试着只读 loadstring string

java runtime-error nosuchelementexception
© www.soinside.com 2019 - 2024. All rights reserved.