I want to order an ArrayList of strings by length, but not just in numeric order. Say for example, the list contains these words: They need to be ordered by their difference in length to a special string, for example: So the final list would look like this (difference in brackets): Answer Use a custom compara…
Tag: java
Why no tail() or head() method in List to get last or first element?
I recently had a discussion with a collegue why the List interface in Java doesn’t have a head() and tail() method. In order to implement such a functionality would have to write a wrapper that looked something like this: I’m not aware of all List implementations but I assume that at least in Link…
How to grab all words that start with capital letters?
I want to create a Java regular expression to grab all words that start with a capital letter then capital or small letters, but those letters may contain accents. Examples : Where Àdónde Rápido Àste Can you please help me with that ? Answer Regex: Java string: Explanation: Caveat: This only works correctly i…
ant build.xml file doesn’t exist
After the installation of my ant in my windows 7 . In cmd i typed ant -v it’s given the ant version but it says the following also. What’s the problem in the system. How i can rectify this issue? Answer You should use ant -version command instead. The -v option is equivalent of -verbose option. Se…
How can I find out which button was clicked?
I’ve got my buttons working right, and I’m a listener to each button like this: Here as you can see the listener is called, and I want to find out which button I’m clicking. Is there a way to do that? I need some way to find the button in the array. Answer try this
How to convert comma-separated String to List?
Is there any built-in method in Java which allows us to convert comma separated String to some container (e.g array, List or Vector)? Or do I need to write custom code for that? Answer Convert comma separated String to List The above code splits the string on a delimiter defined as: zero or more whitespace, a…
Sending Java POST request without calling getInputStream()
I would like to send a POST request in Java. At the moment, I do it like this: I do not understand why I have to call con.getInputStream() in order to actually send the request. If I do not call it, the request is not sent. Is there a problem using PrintStream? It should not matter if I take a
Is it possible to create variables at runtime in Java?
For example, say I wanted to “extract” String[] fruits = {“Pear”, “Banana”, “Apple”}; into three separate variables, eg: How could I do that, ignoring the “Why the heck would you want to do that?” question that you might be urged to ask me. Similar q…
Cannot find class even when jar file is in working directory
I am struggling to get my Java program to run on AIX. I used Eclipse on Windows to create a runnable Jar file, jRams.jar below. I kept on getting a class not found error, until finally I put all the external libraries in the same directory. Still, I get the class not found error. jremote.jar definitely contai…
What’s the best way to load a JSONObject from a json text file?
What would be the easiest way to load a file containing JSON into a JSONObject. At the moment I am using json-lib. This is what I have, but it throws an exception: The output is: Answer try this: and this is your sample-json.txt , should be in json format