Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

only entering numbers

Posted on 2002-03-19
8
Medium Priority
?
170 Views
Last Modified: 2010-03-31
i'm asking the user to enter a number like entering his salary..how can i validate that the user it entering only a number.. to go on with the processes.. like if he enteres anything by mistake..an error message will appear.. but how to do the validation thing here..

waiting..
0
Comment
Question by:saroosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 200 total points
ID: 6881305
something like

try {

   Integer.parseInt("12345");
   Float.parseFloat("12345.50");

} catch (NumberFormatException nfe) {
   // invalid string
}

0
 
LVL 4

Expert Comment

by:kylar
ID: 6881363
If you're using Swing, create a document that doesn't allow the user to enter any non-digit characters and use a JTextField that utilizes your new document.

Kylar
0
 

Expert Comment

by:pv_raman
ID: 6881688
If u are using TextField component,U can add a KeyListener to the text field.for each character verify whether that char is in between 0 and 9.
for eg, ch >=0 && ch <= 9, then only you allow user to enter, otherwise display error message to him.
or
create a document that doesnot allow the user to enter other than digits.Take the constructor of JTextField to utilize ur created document object.or use setDocument()

Hope this will help u.

Raman
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Expert Comment

by:pv_raman
ID: 6881689
If u are using TextField component,U can add a KeyListener to the text field.for each character verify whether that char is in between 0 and 9.
for eg, ch >=0 && ch <= 9, then only you allow user to enter, otherwise display error message to him.
or
create a document that doesnot allow the user to enter other than digits.Take the constructor of JTextField to utilize ur created document object.or use setDocument()

Hope this will help u.

Raman
0
 
LVL 1

Expert Comment

by:dyma82
ID: 6882987
bobbit31's is the most suitable solution!
0
 
LVL 1

Expert Comment

by:saurav17
ID: 6888018

Hi saroosh,

Hope u could do that with isNan().


Regards,
K.J.S.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7901223
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: bobbit31
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7962561
Per recommendation, force-accepted.

Netminder
EE Admin
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question