Skip to main content
added info about latest build
Source Link
jjnguy
  • 3k
  • 15
  • 14

The following is a quickly thrown together method that will return the accepted answer percentage of the user with the given ID.

Note: This is using some code that will be available in the next release. Notice how it dramatically reduces complexity, and the length of code.

public static double getAcceptedAnswerPercentage(StackWrapper sw, int userId)
        throws JSONException, IOException, ParameterNotSetException {
    int acceptedCount = 0;
    int answerCount = 0;
    List<Answer> answers = new AnswerFromUserList(sw, (AnswerQuery) new AnswerQuery()
            .setIds(userId));
    for (Answer a : answers) {
        answerCount++;
        if (a.isIsAccepted())
            acceptedCount++;
    }
    return acceptedCount / (double) answerCount;
}

Are you tired of manually paging queries? We were too. Introducing the AutoFetchList!!

The class AutoFetchList will automatically pull the next pages as needed. Looks like a list, acts like a list...must be a list. Just throw it a Query and it will take care of the paging for you.

There is a partial implementation of this in the datastructures package of the latest release. It is partially functional. But, the code above will run and work.

The following is a quickly thrown together method that will return the accepted answer percentage of the user with the given ID.

Note: This is using some code that will be available in the next release. Notice how it dramatically reduces complexity, and the length of code.

public static double getAcceptedAnswerPercentage(StackWrapper sw, int userId)
        throws JSONException, IOException, ParameterNotSetException {
    int acceptedCount = 0;
    int answerCount = 0;
    List<Answer> answers = new AnswerFromUserList(sw, (AnswerQuery) new AnswerQuery()
            .setIds(userId));
    for (Answer a : answers) {
        answerCount++;
        if (a.isIsAccepted())
            acceptedCount++;
    }
    return acceptedCount / (double) answerCount;
}

Are you tired of manually paging queries? We were too. Introducing the AutoFetchList!!

The class AutoFetchList will automatically pull the next pages as needed. Looks like a list, acts like a list...must be a list. Just throw it a Query and it will take care of the paging for you.

The following is a quickly thrown together method that will return the accepted answer percentage of the user with the given ID.

Note: This is using some code that will be available in the next release. Notice how it dramatically reduces complexity, and the length of code.

public static double getAcceptedAnswerPercentage(StackWrapper sw, int userId)
        throws JSONException, IOException, ParameterNotSetException {
    int acceptedCount = 0;
    int answerCount = 0;
    List<Answer> answers = new AnswerFromUserList(sw, (AnswerQuery) new AnswerQuery()
            .setIds(userId));
    for (Answer a : answers) {
        answerCount++;
        if (a.isIsAccepted())
            acceptedCount++;
    }
    return acceptedCount / (double) answerCount;
}

Are you tired of manually paging queries? We were too. Introducing the AutoFetchList!!

The class AutoFetchList will automatically pull the next pages as needed. Looks like a list, acts like a list...must be a list. Just throw it a Query and it will take care of the paging for you.

There is a partial implementation of this in the datastructures package of the latest release. It is partially functional. But, the code above will run and work.

added info about autofetchlist
Source Link
jjnguy
  • 3k
  • 15
  • 14

The following is a quickly thrown together method that will return the accepted answer percentage of the user with the given ID.

Note: This is using some code that will be available in the next release. Notice how it dramatically reduces complexity, and the length of code.

public static double getAcceptedAnswerPercentage(StackWrapper sw, int userId)
        throws JSONException, IOException, ParameterNotSetException {
    AnswerQuery query = new AnswerQuery();
    int page = 1;
    int pageSizeacceptedCount = 50;
    query.setPage(page).setPageSize(pageSize).setIds(userId);0;
    int acceptedCountanswerCount = 0;
    intList<Answer> answerCountanswers = 0;
 new AnswerFromUserList(sw, (AnswerQuery) whilenew AnswerQuery(true) {
        List<Answer> answers = sw .getAnswersByUserIdsetIds(queryuserId));
        for (Answer a : answers) {
            answerCount++;
            if (a.isIsAccepted())
                acceptedCount++;
        }
        if (answers.size() < pageSize)
            break;
        query.setPage(++page);
    }
    return acceptedCount / (double) answerCount;
}

Note howAre you tired of manually paging queries? We were too. Introducing the query object is used over and over againAutoFetchList!!

The class AutoFetchList will automatically pull the next pages as needed. Looks like a list, acts like a list...must be a list. Just throw it a Query and is updated each time init will take care of the iterationpaging for you.

The following is a quickly thrown together method that will return the accepted answer percentage of the user with the given ID.

public static double getAcceptedAnswerPercentage(StackWrapper sw, int userId)
        throws JSONException, IOException, ParameterNotSetException {
    AnswerQuery query = new AnswerQuery();
    int page = 1;
    int pageSize = 50;
    query.setPage(page).setPageSize(pageSize).setIds(userId);
    int acceptedCount = 0;
    int answerCount = 0;
    while (true) {
        List<Answer> answers = sw.getAnswersByUserId(query);
        for (Answer a : answers) {
            answerCount++;
            if (a.isIsAccepted())
                acceptedCount++;
        }
        if (answers.size() < pageSize)
            break;
        query.setPage(++page);
    }
    return acceptedCount / (double) answerCount;
}

Note how the query object is used over and over again, and is updated each time in the iteration.

The following is a quickly thrown together method that will return the accepted answer percentage of the user with the given ID.

Note: This is using some code that will be available in the next release. Notice how it dramatically reduces complexity, and the length of code.

public static double getAcceptedAnswerPercentage(StackWrapper sw, int userId)
        throws JSONException, IOException, ParameterNotSetException {
    int acceptedCount = 0;
    int answerCount = 0;
    List<Answer> answers = new AnswerFromUserList(sw, (AnswerQuery) new AnswerQuery()
            .setIds(userId));
    for (Answer a : answers) {
        answerCount++;
        if (a.isIsAccepted())
            acceptedCount++;
    }
    return acceptedCount / (double) answerCount;
}

Are you tired of manually paging queries? We were too. Introducing the AutoFetchList!!

The class AutoFetchList will automatically pull the next pages as needed. Looks like a list, acts like a list...must be a list. Just throw it a Query and it will take care of the paging for you.

Post Made Community Wiki
Source Link
jjnguy
  • 3k
  • 15
  • 14

The following is a quickly thrown together method that will return the accepted answer percentage of the user with the given ID.

public static double getAcceptedAnswerPercentage(StackWrapper sw, int userId)
        throws JSONException, IOException, ParameterNotSetException {
    AnswerQuery query = new AnswerQuery();
    int page = 1;
    int pageSize = 50;
    query.setPage(page).setPageSize(pageSize).setIds(userId);
    int acceptedCount = 0;
    int answerCount = 0;
    while (true) {
        List<Answer> answers = sw.getAnswersByUserId(query);
        for (Answer a : answers) {
            answerCount++;
            if (a.isIsAccepted())
                acceptedCount++;
        }
        if (answers.size() < pageSize)
            break;
        query.setPage(++page);
    }
    return acceptedCount / (double) answerCount;
}

Note how the query object is used over and over again, and is updated each time in the iteration.