Skip to main content
1 of 2

##Search Example

You can search for questions with StackWrap4J by instantiating a StackWrapper then calling the search method. Before calling search, you have to create a SearchQuery, which is a simple parameter object.

StackWrapper so = new StackOverflow();

SearchQuery query = new SearchQuery();
query.setTags("swing");
query.setNotTagged("java");
query.setPageSize(10);

List<Question> questions = so.search(query);
System.out.println("Questions: " + questions.size());

for(Question q : questions) {
    System.out.println("Id " + q.getPostId() + ":\t" + q.getTitle());
    System.out.println("Tags: " + q.getTags());
}

If you don't set the tags, nottagged, or intitle parameter in the SearchQuery, the search method will throw an exception.

If you need to modify your search, you can reuse the same query. If I decide I want to add the [scala] tag to the search I did above, I could just add the tag and call search again.

query.addTag("scala");
questions = so.search(query);
...