This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Beware that you might need to clarify just what kind of numbers you need to consider. What about the following? -1 +1 1.0 0.1 .1 1E5 1.23e5 12,345 123456879012 (too big for int) 123456879012345678901234567890 (too big for long) 01 0x7F3A Your problem will be much simpler if you decide you don't need to handle these cases. Or if you at least limit the number of cases you need to handle. But it may be that you are required to handle some of these cases, in which case, don't overlook them. If you do need more here, it's probably not really a Beginner-level problem anymore, so let us know if that's the case and we can move this post to another forum.
"I'm not back." - Bill Harding, Twister
Joined: Nov 07, 2003
holy crow i didnt know it'd be that complex.. thought i was dealing with a problem here with a really easy answer that i just couldn't figure out. what i'm actually dealing here is we're doing error checking to see if the user has inputted a valid info: in the fields first_name and last_name, we're just asked to check if someone has mistakenly inputted a number into those fields... in my own thinking, i think what i would do is to get the decimal value out of every character in the string and see if it is within a range of the decimal values of the letters in the alphabet.. if it is not, then i would say there's an error. i posted this in the beginner's forum hoping someone could give me an easy answer cuz there might probably be one.. ~ rockster
Joined: Dec 10, 2001
A simple regular expression match might be nice to use instead.
Joined: Jan 30, 2000
holy crow i didnt know it'd be that complex.. thought i was dealing with a problem here with a really easy answer that i just couldn't figure out. It's actually not that complex, now that you've provided more info. If you're trying to determine if a name is valid, couldn't you just test to see if the name contains any digits? It doesn't matter which of the formats I listed above is used - if there's any digit at all, the name is invalid. E.g. Jim7 or Go4Broke is not a name. This can be easily tested using the methods Marily and Dirk pointed out earlier.