我正在尝试验证来自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:UsersHpDesktopjproject>javac -cp hibernatejar* *.java
Note: DBHandler.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:UsersHpDesktopjproject>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)
当文本字段为空时,它将获得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());
Komal在这里,正如我所看到的,您希望验证卷号的JTextField,即只应包含整数,不应接受任何其他字符而不是数字。。。好吧,我建议您尝试使用KeyEvent的KeyListener验证每个keyRelease。你按下的每个键都会被验证,如果它的数字是好的,如果不是,它会显示对话框,说";只接受数字";。
我正在分享我的代码,我希望它能帮助
//********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
}
});