java - Jsoup ArrayList and LinkedHashMap Combo -


the code i've written uses jsoup go site, views paragraph headings, saves them arraylist called headinglist. here's tricky part. i've got map takes strings keys , arraylists values. code designed in way requires go more 1 page. because of this, headings amount may vary paragraph amount tied headings. so, idea here create 2 int values. 1 int value called headingamt set after views page , determines how many headings there are. second int value called headcount initialized value of 1. i'm trying set while loop this: while(headcount != headamt + 1) , increment @ end of loop terminates when headcount goes through each heading. during while loop i'm trying go through , add each paragraph arraylist called items, take what's in items arraylist , set values first item in map. then, clear arraylist, go next paragraph save what's there items set arraylist values second item in map , on , on. have code can post, it's confusing since while loop in question has been rearranged many times since can't work properly.

edit here's code in case can help:

public class finder {  public finder(string url) {     string mainurl = "http://www.website.com";     map<string, list<string> > headmap  = new hashmap<>();     arraylist<string> headinglist = new arraylist<>();     arraylist<string> items = new arraylist<>();     int headcounter = 1;      string itemlist = "div > div:nth-child(1).category > ul:nth-child(2) > li.item > span";     int headamt;        document doc1 = null;       ///// connect site menu /////     try{         doc1 = jsoup.connect(url).useragent("mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/42.0.2311.135 safari/537.36")                 .referrer("http://www.google.com")                 .get();     }     catch(ioexception e){         system.out.println("can't connect website");     }          /////// headings ////////         elements head = doc1.select("div > div > div > h3");          ////// loop through headings , add arraylist /////         for(element e: head){             headinglist.add(e.text());          }         headamt = headinglist.size();          /*         here problem          */          while(headcounter != headamt + 1){              elements elem = doc1.select("div > div:nth-child("+ headcounter +").category > ul:nth-child(2) > li.item > span");               (string key : headinglist) {                 for(element e : elem){                 items.add(e.text());                 }                  list<string> value = new arraylist<>(items);                 headmap.put(key, value);             }              items.clear();             headcounter++;             }             }         }     } } 

you can try this:

public class finder { public static void main(string[] args) {     new finder(             "http://www.allmenus.com/ny/new-york/250087-forlinis-restaurant/menu/"); }  public finder(string url) {     document doc1 = null;     try {         doc1 = jsoup                 .connect(url)                 .useragent(                         "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/42.0.2311.135 safari/537.36")                 .referrer("http://www.google.com").get();     } catch (ioexception e) {         system.out.println("can't connect website");     }     elements elements = doc1.select(".category");     hashmap<string, arraylist<list<string>>> menu = new hashmap<string, arraylist<list<string>>>();     (element e : elements) {         string name = e.select(".category_head>h3").first().text();         elements itms = e.select("ul > li");         arraylist<list<string>> menuitems = new arraylist<list<string>>();         (element : itms) {             menuitems.add(arrays.aslist(new string[] {                     it.select("span").first().text(),                     it.select("span").eq(1).text() }));         }         menu.put(name, menuitems);      }     (string key : menu.keyset()) {         system.out.println(key);         arraylist<list<string>> lst = menu.get(key);         (list<string> item : lst) {             system.out.println("       " + item.get(0) + " " + item.get(1));         }         system.out.println("\n");     } } } 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -