2014-10-19 2 views
0

해시 맵에 두 개의 값을 배치 한 다음 다른 메소드에서 액세스하는 코드가 있습니다. 하나의 값 "개"를 반복하지만 메서드의 끝에서 해당 "개"값과 관련된 "레이스"를 인쇄해야합니다 ...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())"과 비슷한 것을 사용할 수 있습니까? 예 : String race : data.keySet())?

이전 방법 :

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

+0

소개 HashMap의 값 부분? – MJSG

+0

경주가 어디 있습니까? 'data' hashmap에 어떻게 데이터를 넣으시겠습니까? 아마 당신은'data.entrySet()'을 반복 할 필요가있을 것이다. 더 많은지도를 읽어보십시오. –

답변

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

안녕하세요, 답변 해 주셔서 ... 구문 오류가 발생하여 ';' ":"대신에 ":"하지만, 만약 내가 그렇게한다면 그것은 여전히 ​​효과가없는 것 같은데 ... 어떤 생각입니까? –

+0

완벽한, 정말 고마워요! 좋은 하루 되세요! :) –

관련 문제