1

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 that doSomethingElse() should be hit.

Is there any inbuilt (better) function to the thing I want to achieve ? Can my code be modified to suit needs ?

12
  • 2
    The problem is probably in getWhatUserEntered(). It should return a full line and not a single token. If you are using Scanner.next, change it to nextLine. Commented Oct 19, 2015 at 8:41
  • indeed show what is getWhatUserEntered(); Commented Oct 19, 2015 at 8:43
  • At present getWhatUserEntered() is hard-coded with values I gave in the code snippet. So no problem there Commented Oct 19, 2015 at 8:44
  • @UmNyobe It is returning harcoded values as String. The values are same as I pasted in the code snippet Commented Oct 19, 2015 at 8:44
  • 1
    @HRgiger, Yeah, I agree with you that one should not use try catch for logic implementation Commented Oct 19, 2015 at 9:24

3 Answers 3

4

Everything is OK due to specific DecimalFormat implementation. JavaDoc says:

Parses text from the beginning of the given string to produce a number. The method may not use the entire text of the given string.

So you have to fix your code to something like this:

  String userInput = getWhatUserEntered();
    try {
        NumberFormat formatter = NumberFormat.getInstance();
        ParsePosition position = new ParsePosition(0);
        Number number = formatter.parse(userInput, position);
        if (position.getIndex() != userInput.length())
            throw new ParseException("failed to parse entire string: " + userInput, position.getIndex());
        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;
Sign up to request clarification or add additional context in comments.

Comments

3

You better use some regex e.g. userInput.matches("[0-9]+") for matching numbers only

5 Comments

The question says any number can be entered. This regex doesn't check for +, -, e, ., etc.
What if the number contains a decimal marker or thousands separator?
well that can be fixed... But regex is an solution to the problem, isn't it? However, "-?\d*\.?\d+e[+-]?\d+" may solve the problem for scientific notation
No. What if the number is entered with a Locale you're not even aware of? The question specifying <LocaleHere> even hints that you should not use regexes.
indeed, there might be cases where regexs are not feasible
2

DecimalFormat accepts any string if it starts with a number.

What you can do is perform an additional check.

try {
  DecimalFormat decimalFormat = (DecimalFormat)     
  NumberFormat.getNumberInstance(<LocaleHere>);
  Number number = decimalFormat.parse(userInput);
  if (number.toString().equals(userInput)) {
    doSomething(number);    // If I reach here, I will doSomething   
    return;
  }
}

4 Comments

Ok. I asked, is there is way for me to achieve the thing in need, using some inbuilt function ?
I initially wrote the very basics of the answer. Now, I edited it to add a bit more.
Thanks. Since @usual developer gives more information in answer and has answered before you, so I would accept his as an answer. Thanks for your help
Indeed, his answer is better than mine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.