Skip to content

Tag: java

Adding whitespace in Java

There is a class trim() to remove white spaces, how about adding/padding? Note: ” ” is not the solution. Answer I think you are talking about padding strings with spaces. One way to do this is with string format codes. For example, if you want to pad a string to a certain length with spaces, use s…

Set running time limit on a method in java

I have a method that returns a String. Is it possible that after a certain time, if threshold is excedeed for that method, to return some specific string? Answer The Guava library has a very nice TimeLimiter that lets you do this on any method that’s defined by an interface. It can generate a proxy for …

Get page content from Apache Commons HTTP Request

So I’m using Apache Commons HTTP to make a request to a webpage. I cannot for the life of me figure out how to get the actual content from the page, I can just get its header information. How can I get the actual content from it? Here is my example code: Answer Use HttpResponse#getEntity() and then Http…

How to do method overloading for null argument?

I have added three methods with parameters: When I am calling doSomething(null) , then compiler throws error as ambiguous methods. So is the issue because Integer and char[] methods or Integer and Object methods? Answer Java will always try to use the most specific applicable version of a method that’s …

How can I scan a maven repository?

I’m developing a code-sharing plugin for eclipse (for a bachelor thesis project). Currently I’m trying to scan a maven repository and generate a package list. I can download and parse a pom.xml using the maven.model classes, but I can’t figure out which maven classes are responsible for pars…