Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If statement

Status
Not open for further replies.

tberger1

MIS
Sep 12, 2006
16
US
I am having a block - I am writing a program that will receive a series of numbers entered in by the user. I need to test to make sure that the number is not negative but also that I truly have a number (not a letter or special character). The first part is easy

if (numbEntered[x] <0)
throw new Exception("enter a number 0 or above")

what I am not sure is how to test for to make sure they entered a number.

Any help is appreciated.
TJ
 
Hi

tberger1 said:
how to test for to make sure they entered a number
Probably you read the user's input as [tt]String[/tt]. So use [tt]Integer.parseInt()[/tt] ( or [tt]Double.parseDouble()[/tt] or whatever you properly need ) on that [tt]String[/tt]. If it throws a [tt]NumberFormatException[/tt], then the value is not numeric.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top