Beware the locale
Today I was programming a toString method for a class widely used in a application, using the very useful String.format that provides a C’s like printf formatter. @Override public String toString() {...
View ArticleThe Caps Lock Java Socket Server
Here is a simple server for those who are starting studying sockets or just needs a simple socket server example for reuse while writing your own behavior. Features: A client should enter a string and...
View ArticleGetting enviroment information on Android
This is a simple program I wrote called Who Am I that shows informations about the device which it is running. Which can be useful for developers and maybe advanced users. Download: WhoAmI.tar.bz2 –...
View ArticleIterating over a HashMap
Iterating over a HashMap using the enhanced loop (foreach) in Java is a good way to keep your code smaller, more legible and usually more semantically coherent. import java.util.HashMap; import...
View Articlecalling commands in Java
I don’t like the approach of calling native shell commands in any language instead of using multi platform libraries, but here is a little prof of concept Java program to call native commands. import...
View ArticleJava: invoking a method by name
import java.lang.reflect.*; public class Foo { public void bar(int param){ System.out.println(param); } public static void main(String args[]){ Object f = new Foo(); try { Method m =...
View ArticleAndroid screen height and width
Context ctx = getContext(); Display display = ((WindowManager)ctx.getSystemService(ctx.WINDOW_SERVICE)).getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); Yes,...
View ArticleJava, printing arrays
As I keep forgetting, this post is to remind me that Java Java doesn’t have a pretty toString() for arrays objects, and it does for Lists. import java.util.List; import java.util.ArrayList; import...
View ArticleRegex with negatives lookahead and lookbehind
“Looking different directions” by Paul Kline. Problem: Match strings that contains a single quotation mark ('), but not multiple ones together. Solution: (?<!')'(?!') This is a regex for a single...
View Articleparallel counting in Java using AtomicInteger
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.TimeUnit; class ParallelCounterExample...
View Article