1

摇摆工作我在对具有值的文本字段进行简单验证时遇到了困难 - 一个 int 变量。我需要以下内容:

  • 只允许数字
  • 不允许值为 0 或以下
  • 必须强制填写。
private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) 

{                                         
        // TODO add your handling code here:

   User controller=new User(

   new Integer(textuid.getText().trim()),textname.getText(),
   textpassword.getText(),textrepassword.getText());
   controller.saveUser(db.getCon());

   // int a= new Integer(textuid.getText().trim());
   System.out.print(controller.getUid());

   if(Integer.parseInt(textuid.getText()) <=0){
      JOptionPane.showMessageDialog( textuid, "Must enter a value greater than zero"); }
   if(textuid.getText().equals("s")){
           JOptionPane.showMessageDialog( textuid, "Must enter a value greater than zero"); }
//        if(textname.getText().equals(""))
//            {
//           JOptionPane.showMessageDialog( textname, "Name not empty"); 
//        }
//           if(textpassword.getText().equals("")){
//           JOptionPane.showMessageDialog( textname, "Password not empty"); 
//        }
//           if(textrepassword.getText().equals("")){
//           JOptionPane.showMessageDialog( textname, "Password not empty"); 
//        }
    }          

显示错误信息

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.<init>(Integer.java:660)
at com.smart.ui.UserForm.btnsaveActionPerformed(UserForm.java:149)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6267)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

我无法解决这个问题。请帮忙。这是我的第一篇文章

4

0 回答 0