0

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>();
}

1 Answer 1

1

You can try as below

  • Create 2 separate methods - method1 and method2 - one will for grabbing id and type and another will for name.
  • Segregate the code present in existing getDetails method and move it into these methods.
  • Create getters and setters for id, type and name inside Kad class.
  • Create a Kad class instance and call method1 and method2 on it and call the respective setters inside these method1 & method2 to set the value for these fields.

Once, method1 and method2 are executed - you have id, type and name values set into three different fields which can be accessed using the getters.

Sign up to request clarification or add additional context in comments.

4 Comments

So, you had me all the way until Step 4. "Inside these method1 and method2" is the bit I dont comprehend.
Also, I'm confused on how I would pull a specific "Kad" out of the ArrayList to append the name to? Pretty much method 1 will be getting all of the ids and types and throwing those into an ArrayList that will output something like: {9 : food, 10 : food, 11 : food} and I'd need to run method2 to append (for example ID 9) the name of the item to it's entry in the ArrayList so it reads: {9 : food : NAME}
try assembling the data in two list inside method one and two. One list for id and type and another one for name. Once you have these list, then read the data from these list in order to create Kads object and prepare kads arraylist.
I have updated my original post with an update and a new problem. Could you help me out as well?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.