Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2003

Tip: Looking for answers? Try searching our database.

dummy proof

Thread view: 
MasterChief - 31 Oct 2003 06:22 GMT
hi,
i'm a newbie in java programming but a great enthusiast. right now i'm
writing the game of Nim and i'm having a hard time finding how to dummy
proof the input that the player is required to enter. actually i've done the
range check (NOT below 1 and NOT above the half of the total) but what i
want to include also is check against _double_. ? How do u do that? Cause in
the whole program i'm using _int_s. Thanks in advance!
Marco Schmidt - 31 Oct 2003 06:33 GMT
MasterChief:

>i'm a newbie in java programming but a great enthusiast. right now i'm
>writing the game of Nim and i'm having a hard time finding how to dummy
>proof the input that the player is required to enter. actually i've done the
>range check (NOT below 1 and NOT above the half of the total) but what i
>want to include also is check against _double_. ? How do u do that? Cause in
>the whole program i'm using _int_s. Thanks in advance!

Not sure what exactly you are trying to validate, but you can make
sure a String is a valid int or double by calling Integer#parseInt or
Double#parseDouble and catching a NumberFormatException.

Regards,
Marco
Signature

Please reply in the newsgroup, not by email!
Java programming tips: http://jiu.sourceforge.net/javatips.html
Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html

MasterChief - 31 Oct 2003 06:49 GMT
> Not sure what exactly you are trying to validate, but you can make
> sure a String is a valid int or double by calling Integer#parseInt or
> Double#parseDouble and catching a NumberFormatException.

Ok, when the player is asked to enter a number, the program expects INT, but
if I enter 0.5 (double) i get whole bunch of errors... how can i write the
code to check for an integer?
i hope i was clearer this time! :)))
Thanks anyway, appreciate it!
Marco Schmidt - 31 Oct 2003 07:32 GMT
MasterChief:

>Ok, when the player is asked to enter a number, the program expects INT, but
>if I enter 0.5 (double) i get whole bunch of errors... how can i write the
>code to check for an integer?

If all you can accept are int values, just use parse approach I
mentioned. Something like this:

String userInput = ...; // get user string
int value;
try {
 value = Integer.parseInt(userInput);
} catch (NumberFormatException nfe) {
 System.err.println("Not a valid int value: " + userInput);
 System.exit(1);
}
// if execution gets to this point, user has entered a valid int
// which is now in value

Regards,
Marco
Signature

Please reply in the newsgroup, not by email!
Java programming tips: http://jiu.sourceforge.net/javatips.html
Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html

nos - 31 Oct 2003 16:49 GMT
or you could use Double.parseDouble() which
handles int and double without error, then convert
result to int

> MasterChief:
>
[quoted text clipped - 22 lines]
> Java programming tips: http://jiu.sourceforge.net/javatips.html
> Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
Roedy Green - 31 Oct 2003 20:26 GMT
>but what i
>want to include also is check against _double_.

http://mindprod.com/converter.html
shows you how to convert Strings to int s or Strings to doubles.

to see if a double is an int, convert it to int and compare.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.