-
JtextField input value
Hi Friends,
I am new to java.Can u Plz help me.I would like to know if there is any way to check if user input into a JtextField is numeric or not. There does not seem to be a method available that is similar to VB's IsNumeric.
Thanks
Diana
-
One way is to use try/catch
Code:
try {
int i=Integer.parseInt(txtF.getText().trim());
} catch (NumberFormatException e) {
System.err.println(txtF.getText().trim()+" is not an integer");
}
Note: when converting string values to numeric you must get rid of any leading/trailing
blanks, use the trim() method of the String class for that.
eschew obfuscation
-
Hi
it is working. but when ever i run the program in the console this is printing
java.lang.NumberFormatException: For input string: "b"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1213)
at java.lang.Float.parseFloat(Float.java:204)
at com.icope.application.ItemDetails.actionPerformed(ItemDetails.java:244)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1764)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1817)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:419)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5134)
at java.awt.Component.processEvent(Component.java:4931)
at java.awt.Container.processEvent(Container.java:1566)
at java.awt.Component.dispatchEventImpl(Component.java:3639)
at java.awt.Container.dispatchEventImpl(Container.java:1623)
at java.awt.Component.dispatchEvent(Component.java:3480)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3165)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095)
at java.awt.Container.dispatchEventImpl(Container.java:1609)
at java.awt.Window.dispatchEventImpl(Window.java:1590)
at java.awt.Component.dispatchEvent(Component.java:3480)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:99
is there is any way to avoid this. My code is
public void actionPerformed(ActionEvent event) {
try {
float priceItem=Float.parseFloat(price.getText().trim());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this,"Invalid Entry. Try again ", "Error Message", JOptionPane.ERROR_MESSAGE);
price.setText("");
}
if(event.getSource() == Save){
if(PriceItem.length()==0) {
JOptionPane.showMessageDialog(this,
"Please Enter the Price", "Error Message",
JOptionPane.ERROR_MESSAGE);
}
else if(price1 <=0 ) {
JOptionPane.showMessageDialog(this,
"Price cannot be Less than or Equal to 0.", "Error Message",
JOptionPane.ERROR_MESSAGE);
price.setText("");
}
else if(reorderlevel.length() ==0){
JOptionPane.showMessageDialog(this,
"Please Enter Reorder Level", "Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
Last edited by daina; 09-26-2005 at 06:06 AM.
-
Is the following code you posted at line 244 in ItemDetails? (ItemDetails.java:244)
try {
float priceItem=Float.parseFloat(price.getText().trim()) ;
} catch (NumberFormatException e) {
-
Test for save button first, if ok then get trimmed string->number value, if exception
then repromt for correct entry, if not then set priceItem value.
Repeat for other numeric values or make a method for it.
Last edited by sjalle; 09-26-2005 at 09:50 AM.
eschew obfuscation
-
Why wasn't the try{ }catch(){} catching the error?
-
Hi Friends,
it worked. thanks for the help.
Regards
Diana
Similar Threads
-
By AM003295 in forum VB Classic
Replies: 4
Last Post: 06-16-2005, 12:47 AM
-
Replies: 5
Last Post: 06-04-2004, 01:08 PM
-
By adohelp in forum Database
Replies: 1
Last Post: 01-09-2002, 07:23 PM
-
By adohelp in forum VB Classic
Replies: 0
Last Post: 12-26-2001, 11:32 AM
-
By N. Richards in forum VB Classic
Replies: 1
Last Post: 09-09-2000, 05:50 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks