Hi,
I am beginner in java programming.
I want to create a program that will enable the user to input data?
I want to check if the input is numeric or not.
How to do it?
thanks!!!

Recommended Answers

All 5 Replies

Try to parse the user input as a number; if it fails, the given string is not a valid number, otherwise it is. Look into the parseXXX method of the wrapper classes (Integer, Long, Float etc.).

That's correct. Notice, however, that you're going to have to commit an act of minor barbarism, using a try/catch as a conditional. That is, you're going to put the parseInt in a try block, and if it returns an exception, you're going to take that as a "false". Since there isn't an "isInteger()" method, this is what you have to do, unless you write your own method for the purpose. Generally, you don't want to write code this way - exceptions should handle exceptional conditions, not expected ones - but that's the way the Integer class is written.

If you want to learn something more about why this is, do some reading about exception handling. A little quality time with google should see you sorted on this.

Generally, you don't want to write code this way - exceptions should handle exceptional conditions, not expected ones - but that's the way the Integer class is written.

I know that, but if the API exposed by the core library says that parsing an invalid input for integer results in an exception, that's how the client should handle it rather than trying to find ways to get around this fact, unless there is a good reason to believe that a custom implementation would make much more sense than the de-facto approach.

If you want to learn something more about why this is, do some reading about exception handling. A little quality time with google should see you sorted on this.

Not sure if this was intended for me or the OP but if it's for me, I think I've done a good amount of reading and am aware of *when* and *how* exceptions should be used. :-)

Sorry, aimed at the OP. I was pretty sure you were aware of this. :)

Just to be clear, I agree with you that this is the right way to do this. I just don't want the OP to get the idea that you would use exceptions in this fashion if there were a sensible alternative.

I worry about someone seeing that example, and coming up with something like this:

Thing[] things = getArrayOfThingsFromSomewhere();
int i = 0;
while (true)
{
  try {Thing t = things[i++];
  }
  catch (ArrayIndexOutOfBoundsException e)
  {
     break;
  }
  t.doSomething();
}

We wouldn't want that, would we? :) (okay, that's a little extreme, but I've actually seen some code, only slightly less bad, using that sort of logic)

<ot>

I would that, but with recrusive methods in finally statement only, then you can call continue; in catch statement instead of break; (break is nonsense in this ...)

anyway this syntax is out from Java, are you **** with deQue too

for (Thing t : things)

</ot>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.