Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

only entering numbers

Posted on 2002-03-19
8
Medium Priority
?
171 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 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
Independent Software Vendors: 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!

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

824 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