2014-10-19 48 views
0

我有代碼,我將兩個值放入一個Hashmap,然後從另一個方法中訪問它們。我通過一個值「狗」迭代,但在方法結束時,我需要打印出與「狗」值有關的「比賽」...從java中的hashmap/keyset獲取值?

這是我到目前爲止:

DecimalFormat df = new DecimalFormat("#.##"); 
    for (String dog: data.keySet()) { // use the dog 



    String dogPage = "http://www.gbgb.org.uk/raceCard.aspx?dogName=" + dog; 
    Document doc1 = Jsoup.connect(dogPage).get(); 
// System.out.println("Dog name: " + dog); 



    Element tblHeader = doc1.select("tbody").first(); 
    for (Element element1 : tblHeader.children()){ 
     String position = element1.select("td:eq(4)").text(); 
     int starts = (position.length() + 1)/4; 
     int starts1 = starts; 
     //   System.out.println("Starts: " + starts); 

     Pattern p = Pattern.compile("1st"); 
     Matcher m = p.matcher(position); 
     int count = 0; 
     while (m.find()){ 
      count +=1; 
     } 
     double firsts = count/(double)starts1 * 100; 
     String firstsStr = (df.format(firsts)); 
     //   System.out.println("Firsts: " + firstsStr + "%"); 

     Pattern p2 = Pattern.compile("2nd"); 
     Matcher m2 = p2.matcher(position); 
     int count2 = 0; 
     while (m2.find()){ 
      count2 +=1; 
     } 
     double seconds = count2/(double)starts1 * 100; 
     String secondsStr = (df.format(seconds)); 
//  System.out.println("Seconds: " + secondsStr + "%"); 

     Pattern p3 = Pattern.compile("3rd"); 
     Matcher m3 = p3.matcher(position); 
     int count3 = 0; 
     while (m3.find()){ 
      count3 +=1; 
     } 

     double thirds = count3/(double)starts1 * 100; 
     String thirdsStr = (df.format(thirds)); 
     //  System.out.println("Thirds: " + thirdsStr + "%"); 

     if (starts1 > 20 && firsts < 20 && seconds > 30 && thirds > 20){ 

      System.out.println("Dog name: " + dog); 
    //  System.out.println("Race: " + race); 
      System.out.println("Firsts: " + firstsStr + "%"); 
      System.out.println("Seconds: " + secondsStr + "%"); 
      System.out.println("Thirds: " + thirdsStr + "%"); 
      System.out.println(""); 
     } 


    } 

我能夠使用類似於「String dog:data.keySet())」來獲得「Race」的值嗎?例如:String race:data.keySet())?

Previous方法:

Document doc = Jsoup.connect(
      "http://www.sportinglife.com/greyhounds/abc-guide").get(); 

    Element tableHeader = doc.select("tbody").first(); 
    Map<String, String> data = new HashMap<>(); 
    for (Element element : tableHeader.children()) { 
     // Here you can do something with each element 
     if (element.text().indexOf("Pelaw Grange") > 0 
       || element.text().indexOf("Shawfield") > 0 
       || element.text().indexOf("Shelbourne Park") > 0 
       || element.text().indexOf("Harolds Cross") > 0) { 
      // do nothing 
     } else { 

      String dog = element.select("td:eq(0)").text(); 
      String race = element.select("td:eq(1)").text(); 
      data.put(dog, race); 

     } 

任何幫助非常感謝,謝謝!

羅布

+1

https://guava-libraries.googlecode.com/svn/tags/release03/javadoc/com/google/common/collect/Multimap.html – SMA 2014-10-19 10:03:52

+0

什麼是HashMap的值部分? – MJSG 2014-10-19 10:11:40

+0

你在哪裏比賽?你如何將數據放入'data'散列圖?可能你需要迭代'data.entrySet()'。閱讀更多abot地圖。 – 2014-10-19 10:13:30

回答

0

我假設HashMap的價值部分是Race

如果是的話,那麼你就可以做到以下幾點:

String race = data.get(dog); 
在當前的代碼

,你正在做以下幾點:

for (String dog: data.keySet()) { // use the dog 
    String race = data.get(dog); // this will give the value of race for the key dog 
    // using dog to do fetch details from site... 
} 

你也可以做到以下幾點:

for (Entry<String, String> entry: data.entrySet()) { 
    String dog = entry.getKey(); 
    String race = entry.getValue(); 
    // using dog to do fetch details from site... 
} 
+0

嗨,感謝您的回答......它似乎給出了一個語法錯誤,說它期待着一個「;」而不是「:」,但如果我這樣做,它似乎仍然不工作......任何想法? – 2014-10-19 10:23:34

+0

完美,非常感謝!祝你有美好的一天! :) – 2014-10-19 11:10:34