【问题标题】:JTextfield Validation for numbers?数字的JTextfield验证?
【发布时间】:2020-11-19 20:01:58
【问题描述】:

我正在尝试从JTextField 验证我的卷号(整数值的输入)。好吧,我的代码正在编译,但是在运行时它给了我一个错误NumberFormatException。 这是我的验证码

public int rno_vd() {
    int a=0,b=0,c=0,x=0,y=0;
    int vrno =Integer.parseInt(txtRno.getText());
    String r = String.valueOf(vrno);
    if (r.isEmpty()) {
        JOptionPane.showMessageDialog(null,"rno should not be empty");
        a=1;
    }
    else if (Pattern.matches("[a-zA-Z]+",r)) {
        JOptionPane.showMessageDialog(null,"rno should be in digits");
        b=1;
    }
    else if (vrno < 0) {
        JOptionPane.showMessageDialog(null,"rno cannot be negative");
        c=1;
    }
    System.out.println(a + b + c);
    if (a==1 || b==1 || c==1) {
        x=1;
        return x;
    }
    else {
        y=0;
        return y;
    }
}

错误

C:\Users\Hp\Desktop\jproject>javac -cp hibernatejar\*  *.java
Note: DBHandler.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

C:\Users\Hp\Desktop\jproject>java -cp hibernatejar\*;.  Sms
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
        at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
        at java.base/java.lang.Integer.parseInt(Integer.java:662)
        at java.base/java.lang.Integer.parseInt(Integer.java:770)
        at AddFrame.lambda$new$1(AddFrame.java:80)
        at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
        at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
        at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
        at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
        at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
        

【问题讨论】:

标签: java hibernate swing validation jtextfield


【解决方案1】:

Komal 在这里,我可以看到您想要验证 JTextField 的滚动编号,即应该只包含整数,不应接受任何其他字符而不是数字... 好吧,我建议您尝试使用 KeyEvent 的 KeyListener 对每个 keyReleased 进行验证。 您按下的每个键都经过验证,如果它的数字是好的,如果不是,它会显示对话框说“只接受数字”。

我正在分享我的代码,希望对您有所帮助

//********function to check if value is numric or not*********
public static boolean isNumeric(String str) { 
        try {  
            Integer.parseInt(str);  
            return true;
        } 
        catch(NumberFormatException e){  
            return false;  
        }  
    }
//************************function ends******************************


//txtRno is my JTextField 

txtRno.addKeyListener(new KeyListener(){
        public void keyPressed(KeyEvent e)
        {
            //code      
        }
        public void keyReleased(KeyEvent e)
        {
            String value = txtRno.getText();
            int l = value.length();
            if(!isNumeric(value) && l>0){//if not numric it will not allow you to edit JTextField and show error message
                txtRno.setEditable(true);
                JOptionPane.showMessageDialog(c,"You need to enter number","ERROR",JOptionPane.ERROR_MESSAGE);
                txtRno.requestFocus();
                txtRno.setText("");
                txtRno.setEditable(true);
                lblError.setText("");
            } 
            else { //else it will take the input as it already number or integer 
                txtRno.setEditable(true);
                lblError.setText("");
            }
        }
        public void keyTyped(KeyEvent e)
        {
            //code
        }
    });

【讨论】:

    【解决方案2】:

    当文本字段为空值时,它正在获取NumberFormatException

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    

    能否先验证文本,然后解析字符串?

        // Validating text input first
        String r = txtRno.getText();
    
        if (r.isEmpty())
            {JOptionPane.showMessageDialog(null,"rno should not be empty");
                a=1;}
        else if (Pattern.matches("[a-zA-Z]+",r))
            {JOptionPane.showMessageDialog(null,"rno should be in digits");
                b=1;}
        else if (vrno < 0)
            {JOptionPane.showMessageDialog(null,"rno cannot be negative");
                c=1;}
    
        // Converting to int, if validation is successful
        int vrno =Integer.parseInt(txtRno.getText());
    

    【讨论】:

    • 我厌倦了这个它不起作用仍然给我同样的错误
    猜你喜欢
    • 1970-01-01
    • 2017-07-20
    • 2012-12-31
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    相关资源
    最近更新 更多