/** * @param ticker The ticker of the stock to look up. * @throws ConnectionException if it has problems connecting to Yahoo. * @return The value of the given stock, as %CompanyName: %Value. */ public String getStockValue(String ticker) throws ConnectionException { try { final HtmlPage lookup = client.getPage( new StringBuilder("http://www.google.com/finance?q=").append(ticker).toString()); final DomElement pricediv = lookup.getFirstByXPath("//div[@id = 'price-panel']/div/span/span"); final DomElement compname = lookup.getFirstByXPath("//div[@class = 'appbar-snippet-primary']/span"); return new StringBuilder(compname.getTextContent()) .append(": ") .append(pricediv.getTextContent()) .toString(); } catch (IOException | FailingHttpStatusCodeException e) { System.out.println( new StringBuilder("Exception in retrieval.") .append("InternetConnection.getStockValue(") .append(ticker) .append(")") .toString()); System.out.println(e.toString()); } throw new ConnectionException( new StringBuilder("Unable to find data for ").append(ticker).toString()); }