Solved

only entering numbers

Posted on 2002-03-19
8
165 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
8 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now