Solved

only entering numbers

Posted on 2002-03-19
8
167 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
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!

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

679 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