• 締切済み

java 検索数表示ができるようにしたいのですが

補足へ import java.io.*; import java.net.*; import java.lang.*; public class secc3 { public static void main(String[] args) { System.out.println("入力"); String keyword = getKeyword(); String htmlSrc = getHTMLSrc("http://search.yahoo.co.jp/search?p=" + keyword, "UTF-8"); String sec = getNumSearchHits(htmlSrc); sec = sec.replace(",",""); int ken = Integer.parseInt(sec); System.out.println(ken); if (ken > 0) { System.out.println("Hit数約" + ken + "件"); } else { System.out.println("結果0件"); } } private static String getKeyword() { String keyword = ""; BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(System.in)); keyword = br.readLine(); } catch (IOException e) { e.printStackTrace(); } finally { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } keyword = keyword.replaceAll(" ", "+"); return keyword; } private static String getHTMLSrc(String strURL, String charSet) { StringBuffer sb = new StringBuffer(); HttpURLConnection conn = null; BufferedReader br = null; try { URL url = new URL(strURL); conn = (HttpURLConnection)url.openConnection(); InputStreamReader isr = new InputStreamReader(conn.getInputStream(), charSet); br = new BufferedReader(isr); String tmp = ""; while ((tmp = br.readLine()) != null) { sb.append(tmp); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } if (conn != null) { conn.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } private static String getNumSearchHits(String htmlSrc) { java.util.regex.Pattern p = java.util.regex.Pattern.compile(" / 約([,0-9]+)件 - "); java.util.regex.Matcher m = p.matcher(htmlSrc); return m.find() ? htmlSrc.substring(m.start(1), m.end(1)) : ""; } }

みんなの回答

  • yamada_g
  • ベストアンサー率68% (258/374)
回答No.1

実行したら 入力 java 195000000 Hit数約195000000件 て表示されましたけど何をどうしたいのですか Hit数と検索数ってちがうのでしょうか