1

Appreciate the patience since I'm still learning. I have a web parsing application I'm building that allows the end user to type in any web address and pull the source html, which is then saved to a text file. I'm now trying to purpose that application specifically for pulling stock data from CNBC markets. I have a scanner I called name, which pulls in the name of the stock the user is searching for. I then need to be able to iterate through a for loop over the length of the array for the string the user enters. When I do this, I get .length cannot be resolved as an error. What is the best alternative I can use? Java doesn't seem to have a simple length function like most languages I have worked with.

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();
        }
    }
}
greg-449
  • 109,219
  • 232
  • 102
  • 145
Zach Rieck
  • 419
  • 1
  • 4
  • 23
  • JavaScript !== Java – VLAZ Jan 10 '20 at 22:42
  • If you mean the 'market.length' line - `market` is a `String`, not an array. Do you mean to loop through `funds`? For a string use `length()`. You are also comparing strings incorrectly - see [here](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – greg-449 Jan 11 '20 at 07:57

0 Answers0