This is going to be a series of articles about things that don’t go smooth when moving from J2SE to developing for J2ME in my case especially MIDP. It’s not intended to blame the guys behind J2ME and the MIDP APIs for what they’ve done – instead it’s intended to be a collection of common problems or traps developers used to J2SE could fall in.
First up: Simple often-used utility functions:
static boolean Boolean.parseBoolean(String s) missing
The lack of this method is really annoying once you have to cope with reading in configuration values. Many of them will be in boolean-like format but you will have no method to parse them into booleans.
A very simple re-write of this method could look like that:
public static boolean parseBoolean(String s) {
return s.equalsIgnoreCase(“true”) || s.equalsIgnoreCase(“yes”);
}
Since it could be so easy to write this function (and Integer.parseInt() still exists which seems more complex to me) I don’t understand why it has not been included.
String[] String.split() missing
Every now and then one will have to parse some input – and this is where split() comes in handy. Would have come. Luckily String.indexOf() exists which allows for the following re-implementation of String.split():
public static String[] split(String source, String separator) {
Vector nodes = new Vector();int index = source.indexOf(separator);
while(index >= 0) {
nodes.addElement(source.substring(0, index));
source = source.substring(index + separator.length());
index = source.indexOf(separator);
}
nodes.addElement(source);String[] result = new String[nodes.size()];
if(nodes.size() > 0) {
for(int i = 0; i < nodes.size(); i++) {
result[i] = (String)nodes.elementAt(i);
}
}return result;
}
All in all this might not sound too bad since both “re-implementations” were pretty easy to write. But: Since Boolean and String are declared abstract you can’t subclass and by that extend those. So you will have to declare a “tool”-class for them – not the most elegant way in my opinion.
So these are methods I think many developers will use very regularly but J2ME/MIDP doesn’t provide them.