There is a requirement that if user enters a number, parse it and doSomething()
.
If user enters a mixture of number and string, then doSomethingElse()
So, I wrote the code as under:
String userInput = getWhatUserEntered();
try {
DecimalFormat decimalFormat = (DecimalFormat)
NumberFormat.getNumberInstance(<LocaleHere>);
Number number = decimalFormat.parse(userInput);
doSomething(number); // If I reach here, I will doSomething
return;
}
catch(Exception e) {
// Oh.. user has entered mixture of alpha and number
}
doSomethingElse(userInput); // If I reach here, I will doSomethingElse
return;
The function getWhatUserEntered()
looks as under
String getWhatUserEntered()
{
return "1923";
//return "Oh God 1923";
//return "1923 Oh God";
}
But, there is a problem.
- When user enters 1923 -->
doSomething()
is hit - When user enters Oh God 1923 -->
doSomethingElse()
is hit - When user enters 1923 Oh God -->
doSomething()
is hit. This is wrong Here I need thatdoSomethingElse()
should be hit.
Is there any inbuilt (better) function to the thing I want to achieve ? Can my code be modified to suit needs ?