Jul 17

The Random class provides a method that returns a random integer between 0 and n (exclusive).


Random wheel = new Random();
int random = wheel.nextInt(n); 

By default it uses the current timestamp as the seed for the random generator, or alternately you can provide your own.

written by objects \\ tags: ,

Jun 11

You can tell the parseInt() method what base number system is used by the String that is being parsed.


String hex = "01001101";
int n = Integer.parseInt(hex, 2);

written by objects \\ tags: , , , , ,

Apr 15

public static String zeroPad(int value, int width) {
   return String.format("%0"+width+"d", value);
}

written by objects \\ tags: , , ,