So, I am trying to make a function that will visit one page, grab the ID and TYPE of an item, then visits a link and plugs in the ID and TYPE to get the ITEM NAME. Then it will add all of this into an ArrayList like. new List(id,type,name);
public static ArrayList<Kad> getDetails(final String strHTML, final String strHTML2) {
final ArrayList<Kad> kads = new ArrayList<Kad>();
try {
final Pattern regex = Pattern
.compile(
"id=(\\d+)\\&tab=([a-zA-Z]+)",
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
final Pattern regex2 = Pattern
.compile(
"font-weight:bold; font-size:11px;\">([\\w\\s]+)</div>",
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
final Matcher regexMatcher = regex.matcher(strHTML);
final Matcher regexMatcher2 = regex2.matcher(strHTML2);
String ids = "";
while (regexMatcher.find() && regexMatcher2.find()) {
final int id = Integer.parseInt(regexMatcher.group(1));
final String tab = regexMatcher.group(2);
final String name = regexMatcher2.group(1);
if (!ids.contains(id + "|") && !ignoreList.contains(id)) {
kads.add(new Kad(id,tab,name));
ids += id + "|";
}
}
return kads;
} catch (final Exception ex) {
ex.printStackTrace();
}
return new ArrayList<Kad>();
}
This code works to get the id, type and name of the item. But, I can't get a list of IDs since this function gets the ID and then the name. What is the best way to break this up into two functions where one gets the ID and TYPE, then the other gets the name.
This method is called using:
data = wrapper.post("LINK1", "tab=" + "food" + "&page=" + "1", "");
data2 = wrapper.post("LINK2", "tab=" + "CURRENT ITEM TYPE IN LIST GOES HERE" + "&id=" + "CURRENT ITEM ID IN LIST GOES HERE", "");
final ArrayList<Kad> kadList = Kad.getDetails(data, data2);
I tried to break it up into two seperate methods, but could not figure out how to add the name found in method 2 to the already created list of ID and TYPE in method 1.
EDIT: Okay, so after the suggested solution below, I can now fetch id and name in two seperate methods. The problem now is that method 2( the get name method) Is giving every single ID the same name(the first name that is searched). How Do i solve this?
public static ArrayList<Kad> findItemName2(final int id, final String tab, final String strHTML) {
final ArrayList<Kad> names = new ArrayList<Kad>();
try {
final Pattern regex = Pattern
.compile(
"font-weight:bold; font-size:11px;\">([\\w\\s]+)</div>",
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
final Matcher regexMatcher = regex.matcher(strHTML);
String ids = "";
while (regexMatcher.find()) {
final String name2 = regexMatcher.group(1);
if (!ids.contains(name2 + "|")) {
names.add(new Kad(id,tab, name2));
name = name2;
ids += name2 + "|";
}
}
return names;
} catch (final Exception ex) {
ex.printStackTrace();
}
return new ArrayList<Kad>();
}