Sunday, March 25, 2007

How will YOU do it?

Hello people,

What's on my mind ?

Don't read too much into the title, for sometime now I was planning to start this. Now that I am finally doing it I expect a lot of active participation from all you guys. I will be putting utility code snippets, which many of us might require in our work life.

Who's the boss ?

It's my blog space that doensn't mean what ever i write you gotta take it. I mentioned above "active participation". So what I will expect is that you guys tell me a better way of achieving the same objective in a better and more effective manner. It will be nice if we can discuss why one way is better than the other.

Why am I doing this ?

Its a known fact that most of "us" write code differently at different point of "time". What code I wrote few years back looks absolute bull shit to me now, I laugh on myself thinking that this could have been done in a better, easier and effective way. What code I write now might look bull shit to you. So i think its a nice way to learn from each others mistakes and thus improve.

Language I am using is Java.

Do comment even if you think this idea to be utter bull shit!!!

So here goes the first and a very simple one :

What's the Issue ?

How do you find whether a string is an Integer or not? Assumptions - String passed is well within int's limit and can have only sign character, no other special character will be handled like comma, dot.

How I do it ?

public static boolean isInteger(String strInt) {

boolean isInteger = false;
try {
Integer.parseInt(strInt);
isInteger = true;
} catch (NumberFormatException nfe) {
isInteger = false;
}catch (Exception e) {
isInteger = false;
} return isInteger;
}

This problem might be infantile one but not a bad one to start with. I am sure people coding in java must have faced the issue - no isNumeric() api in Java like VB.

What I expect ? - Replies, comments, views, more problems, better answers. Just any response.

No comments: