DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2005
    Posts
    22

    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

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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

  3. #3
    Join Date
    Sep 2005
    Posts
    22
    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.

  4. #4
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    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) {

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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

  6. #6
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    Why wasn't the try{ }catch(){} catching the error?

  7. #7
    Join Date
    Sep 2005
    Posts
    22
    Hi Friends,

    it worked. thanks for the help.

    Regards
    Diana

Similar Threads

  1. Understanding Open xxx For Input As #6
    By AM003295 in forum VB Classic
    Replies: 4
    Last Post: 06-16-2005, 12:47 AM
  2. cursor positioning within input boxes?
    By charlie in forum Web
    Replies: 5
    Last Post: 06-04-2004, 01:08 PM
  3. Replies: 1
    Last Post: 01-09-2002, 07:23 PM
  4. Replies: 0
    Last Post: 12-26-2001, 11:32 AM
  5. Discord in Program
    By N. Richards in forum VB Classic
    Replies: 1
    Last Post: 09-09-2000, 05:50 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


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


Sponsored Links