another prb in java


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: another prb in java

  1. #1
    Join Date
    May 2004
    Posts
    29

    another prb in java

    i have no idea how to make sure if whatever the user is an integer or not. i wan the user to enter integers only in a text field. how can i make sure he does?

  2. #2
    Join Date
    May 2004
    Posts
    26
    I think there is some direct method in Java for what you asked, but one easy workaround would be to do this:

    Pass the string that you get from the user (strUser) to the following method:

    Integer.parseInt(strUser);

    If the string is not a decimal integer, NumberFormateException is thrown. Catch this exception and return a message telling the user to enter valid integers.

  3. #3
    Join Date
    May 2004
    Posts
    29
    forgive me for asking you this, but you do know that by text field, i mean TextField txt = new TextField(25) and not the text field that you see when u'r using joptionpane.showinputdialog to enter an integer. right?

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    What difference does it make? a string is a string wherever you get it from. Use the paramString() method to get the string, then parse the string to an integer as Atlas suggested. Do you know how to use a try/catch block? You should do the parsing inside a try block, and then catch a NumberFormatException in the catch block. If you don't know how to use them just ask and we will explain

  5. #5
    Join Date
    May 2004
    Posts
    29
    many thanx. ya i know how to use them. they're used so that you'll catch the exception and make sure ur prg keeps on running rather than having to recompile it again after checking for errors like the default handler does. i'll get to it once am done with my other work. thanx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center