The code I've written uses jsoup to go to a site, views all the paragraph headings, then saves them to an ArrayList called headingList
. Here's the tricky part. I've got a map that takes Strings as the keys and ArrayLists as the values. The code is designed in a way that requires it to go to more than one page. Because of this, the headings amount may vary greatly as well as the paragraph amount that is tied to the headings. So, the idea here was to create two int values. One int value called headingAmt
is set after it views the page and determines how many headings there are. The second int value called headCount
is initialized to a value of 1. Then what I'm trying to do is set a while loop like this: while(headCount != headAmt + 1)
and increment it at the end of the loop so that it terminates when headCount
goes through each heading. During the while loop I'm trying to go through and add each paragraph to an ArrayList called items
, then take what's in the items arrayList and then set it as the values for the first item in the map. Then, clear the ArrayList, go to the next paragraph save what's there to items
then set that ArrayList for the values for the second item in the map and so on and so on. I have code I can post, but it's confusing since the while loop in question has been rearranged so many times since I can't get it to work properly.
Edit Here's the code in case anyone 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 to site to get menu /////
try{
doc1 = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36")
.referrer("http://www.google.com")
.get();
}
catch(IOException e){
System.out.println("Can't connect to website");
}
/////// Get headings ////////
Elements head = doc1.select("div > div > div > h3");
////// Loop through headings and add to ArrayList /////
for(Element e: head){
headingList.add(e.text());
}
headAmt = headingList.size();
/*
Here is the problem
*/
while(headCounter != headAmt + 1){
Elements elem = doc1.select("div > div:nth-child("+ headCounter +").category > ul:nth-child(2) > li.item > span");
for (String key : headingList) {
for(Element e : elem){
items.add(e.text());
}
List<String> value = new ArrayList<>(items);
headMap.put(key, value);
}
items.clear();
headCounter++;
}
}
}
}
}