如何将JTextField输入转换为int以进行验证?(How can I convert JTextField input into int for validation?)
我需要帮助将文本字段输入转换为int以进行验证。 contactnum来自textFieldContactnum字段。 我想将textFieldContactnum的输入转换为int。 我已经尝试将textFieldContactnum.getText()存储到String中,并使用新的int(contactnumber)来解析(Integer.parseInt),如下代码所示。
private void actionPerformedOk() { String username = textFieldUsername.getText(); String password = passwordField.getText(); String email = textFieldEmail.getText(); String securityqn = textFieldSecurityqn.getText(); String securityanswer = textFieldSecurityanswer.getText(); contactnumber = textFieldContactnum.getText(); int contactnum = Integer.parseInt(contactnumber); String firstname = textFieldFirstName.getText(); String lastname = textFieldLastName.getText(); String gender = genderSelected; String nric = textFieldNRIC.getText(); String address = textFieldAddress.getText(); postalcode1 = textFieldPostalCode.getText(); int postalcode = Integer.parseInt(postalcode1); String day = dayList.getSelectedItem().toString(); String month = monthList.getSelectedItem().toString(); String year = yearList.getSelectedItem().toString(); if (!day.equals("Day") && !month.equals("Month") && !year.equals("Year")) { dateAsString = day + "/" + month + "/" + year; SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); try { Date date = sdf.parse(dateAsString); System.out.println(date); } catch (ParseException e1) {} } dateAsString = day + ("/") + month + ("/") + year; String date = dateAsString; // retrieve the user input from the text box/area provided if (validateInput()) { username = textFieldUsername.getText(); if (actions.equalsIgnoreCase("Submit")){ signup = new hiroSIGNUP(username, password, email, securityqn, securityanswer, contactnum, firstname, lastname, gender, nric, address, postalcode, date); actionPerformedCreate(); } else{ // update the values of the current expense object //update empty to some fields signup.setUsername(username); signup.setPassword(password); signup.setEmail(email); signup.setSecurityqn(securityqn); signup.setSecurityanswer(securityanswer); signup.setContactnum(contactnum); signup.setFirstname(firstname); signup.setLastname(lastname); signup.setGender(gender); signup.setNric(nric); signup.setAddress(address); signup.setPostalcode(postalcode); signup.setDate(date); actionPerformedUpdate(); } } } /** * Purpose: This method updates the expense record * in the database. * Input: Nil * Return: void */ public void actionPerformedUpdate(){ // Update record in database and check return value if (SignupDA.updatehiroSIGNUP(signup)) { JOptionPane.showMessageDialog(myFrame, "Record updated successfully", "Alert", JOptionPane.INFORMATION_MESSAGE); // reset text field for next record. textFieldUsername.setEditable(false); passwordField.setEditable(false); textFieldEmail.setEditable(false); textFieldSecurityqn.setEditable(false); textFieldSecurityanswer.setEditable(false); textFieldContactnum.setEditable(false); textFieldFirstName.setEditable(false); textFieldLastName.setEditable(false); dayList.setEditable(false); monthList.setEditable(false); yearList.setEditable(false); textFieldNRIC.setEditable(false); textFieldAddress.setEditable(false); textFieldPostalCode.setEditable(false); } else { JOptionPane.showMessageDialog(myFrame, "Database Error. Record not updated.", "Alert", JOptionPane.ERROR_MESSAGE); } } /** * Purpose: This method creates the expense record * in the database. * Input: Nil * Return: void */ public void actionPerformedCreate(){ // insert to database and check return value if (SignupDA.createhiroSIGNUP(signup)) { JOptionPane.showMessageDialog(myFrame, "Record created successfully", "Alert", JOptionPane.INFORMATION_MESSAGE); // reset text field for next record. // reset text field for next record. textFieldUsername.setText(""); passwordField.setText(""); textFieldEmail.setText(""); textFieldSecurityqn.setText(""); textFieldSecurityanswer.setText(""); textFieldContactnum.setText(""); textFieldFirstName.setText(""); textFieldLastName.setText(""); group.clearSelection(); textFieldNRIC.setText(""); textFieldAddress.setText(""); textFieldPostalCode.setText(""); } else JOptionPane.showMessageDialog(myFrame, "Database Error. Record not created.", "Alert", JOptionPane.ERROR_MESSAGE); } private boolean validateInput() { boolean result = false; String msg = ""; int msgType = JOptionPane.ERROR_MESSAGE; // retrieve the user input from the text box/area provided String username = textFieldUsername.getText(); String password = passwordField.getText(); String email = textFieldEmail.getText(); String securityqn = textFieldSecurityqn.getText(); String securityanswer = textFieldSecurityanswer.getText(); int contactnum = Integer.parseInt(contactnumber); String firstname = textFieldFirstName.getText(); String lastname = textFieldLastName.getText(); String gender = genderSelected; //FIND OUT HOW TO VALIDATE GENDER RADIO BUTTON String nric = textFieldNRIC.getText(); String address = textFieldAddress.getText(); int postalcode = Integer.parseInt(postalcode1); String date = dateAsString; if (username.length() == 0) msg += "Please enter Username.\n"; if (password.length() == 0) msg += "Please enter password.\n"; if (email.length() == 0) msg += "Please enter Email.\n"; if (securityqn.length() == 0) msg += "Please enter Security Question.\n"; if (securityanswer.length() == 0) msg += "Please enter Security Answer\n"; if (contactnumber.length() == 0) msg += "Please enter Contact Number\n"; if (firstname.length() == 0) msg += "Please enter First name\n"; if (lastname.length() == 0) msg += "Please enter Last name\n"; if (genderSelected.length() == 0) msg += "Please enter Gender\n"; if (nric.length() == 0) msg += "Please enter NRIC\n"; if (address.length() == 0) msg += "Please enter Address\n"; if (postalcode1.length() == 0) msg += "Please enter Postal code\n"; if (date.length() == 0) msg += "Please enter Date\n"; if (msg.length() == 0) result = true; else JOptionPane.showMessageDialog(myFrame, msg, "Alert", msgType); return result; } /** * @wbp.parser.constructor */ public hiroSIGNUPPanel(JFrame mf, String action, hiroSIGNUP s){ this(mf, action); //display the current expense information signup = s; textFieldUsername.setText(s.getUsername()); passwordField.setText(s.getPassword()); textFieldEmail.setText(s.getEmail()); textFieldSecurityqn.setText(s.getSecurityqn()); textFieldSecurityanswer.setText(s.getSecurityanswer()); textFieldContactnum.setText(String.valueOf(s.getContactnum())); textFieldFirstName.setText(s.getFirstname()); textFieldLastName.setText(s.getLastname()); //textFieldGender.setText(s.getGender()); textFieldNRIC.setText(s.getNric()); textFieldAddress.setText(s.getAddress()); textFieldPostalCode.setText(String.valueOf(s.getPostalcode()));
}
}
我的错误就是这个
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at hiroApp.ui.hiroSIGNUPPanel.actionPerformedOk(hiroSIGNUPPanel.java:781)
I need help for converting textfield input into int for validation. The contactnum is from the field textFieldContactnum. I want to convert textFieldContactnum's input into int. I've tried storing the textFieldContactnum.getText() into a String first and use a new int (contactnumber) to parse (Integer.parseInt) in like below the codes..
private void actionPerformedOk() { String username = textFieldUsername.getText(); String password = passwordField.getText(); String email = textFieldEmail.getText(); String securityqn = textFieldSecurityqn.getText(); String securityanswer = textFieldSecurityanswer.getText(); contactnumber = textFieldContactnum.getText(); int contactnum = Integer.parseInt(contactnumber); String firstname = textFieldFirstName.getText(); String lastname = textFieldLastName.getText(); String gender = genderSelected; String nric = textFieldNRIC.getText(); String address = textFieldAddress.getText(); postalcode1 = textFieldPostalCode.getText(); int postalcode = Integer.parseInt(postalcode1); String day = dayList.getSelectedItem().toString(); String month = monthList.getSelectedItem().toString(); String year = yearList.getSelectedItem().toString(); if (!day.equals("Day") && !month.equals("Month") && !year.equals("Year")) { dateAsString = day + "/" + month + "/" + year; SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); try { Date date = sdf.parse(dateAsString); System.out.println(date); } catch (ParseException e1) {} } dateAsString = day + ("/") + month + ("/") + year; String date = dateAsString; // retrieve the user input from the text box/area provided if (validateInput()) { username = textFieldUsername.getText(); if (actions.equalsIgnoreCase("Submit")){ signup = new hiroSIGNUP(username, password, email, securityqn, securityanswer, contactnum, firstname, lastname, gender, nric, address, postalcode, date); actionPerformedCreate(); } else{ // update the values of the current expense object //update empty to some fields signup.setUsername(username); signup.setPassword(password); signup.setEmail(email); signup.setSecurityqn(securityqn); signup.setSecurityanswer(securityanswer); signup.setContactnum(contactnum); signup.setFirstname(firstname); signup.setLastname(lastname); signup.setGender(gender); signup.setNric(nric); signup.setAddress(address); signup.setPostalcode(postalcode); signup.setDate(date); actionPerformedUpdate(); } } } /** * Purpose: This method updates the expense record * in the database. * Input: Nil * Return: void */ public void actionPerformedUpdate(){ // Update record in database and check return value if (SignupDA.updatehiroSIGNUP(signup)) { JOptionPane.showMessageDialog(myFrame, "Record updated successfully", "Alert", JOptionPane.INFORMATION_MESSAGE); // reset text field for next record. textFieldUsername.setEditable(false); passwordField.setEditable(false); textFieldEmail.setEditable(false); textFieldSecurityqn.setEditable(false); textFieldSecurityanswer.setEditable(false); textFieldContactnum.setEditable(false); textFieldFirstName.setEditable(false); textFieldLastName.setEditable(false); dayList.setEditable(false); monthList.setEditable(false); yearList.setEditable(false); textFieldNRIC.setEditable(false); textFieldAddress.setEditable(false); textFieldPostalCode.setEditable(false); } else { JOptionPane.showMessageDialog(myFrame, "Database Error. Record not updated.", "Alert", JOptionPane.ERROR_MESSAGE); } } /** * Purpose: This method creates the expense record * in the database. * Input: Nil * Return: void */ public void actionPerformedCreate(){ // insert to database and check return value if (SignupDA.createhiroSIGNUP(signup)) { JOptionPane.showMessageDialog(myFrame, "Record created successfully", "Alert", JOptionPane.INFORMATION_MESSAGE); // reset text field for next record. // reset text field for next record. textFieldUsername.setText(""); passwordField.setText(""); textFieldEmail.setText(""); textFieldSecurityqn.setText(""); textFieldSecurityanswer.setText(""); textFieldContactnum.setText(""); textFieldFirstName.setText(""); textFieldLastName.setText(""); group.clearSelection(); textFieldNRIC.setText(""); textFieldAddress.setText(""); textFieldPostalCode.setText(""); } else JOptionPane.showMessageDialog(myFrame, "Database Error. Record not created.", "Alert", JOptionPane.ERROR_MESSAGE); } private boolean validateInput() { boolean result = false; String msg = ""; int msgType = JOptionPane.ERROR_MESSAGE; // retrieve the user input from the text box/area provided String username = textFieldUsername.getText(); String password = passwordField.getText(); String email = textFieldEmail.getText(); String securityqn = textFieldSecurityqn.getText(); String securityanswer = textFieldSecurityanswer.getText(); int contactnum = Integer.parseInt(contactnumber); String firstname = textFieldFirstName.getText(); String lastname = textFieldLastName.getText(); String gender = genderSelected; //FIND OUT HOW TO VALIDATE GENDER RADIO BUTTON String nric = textFieldNRIC.getText(); String address = textFieldAddress.getText(); int postalcode = Integer.parseInt(postalcode1); String date = dateAsString; if (username.length() == 0) msg += "Please enter Username.\n"; if (password.length() == 0) msg += "Please enter password.\n"; if (email.length() == 0) msg += "Please enter Email.\n"; if (securityqn.length() == 0) msg += "Please enter Security Question.\n"; if (securityanswer.length() == 0) msg += "Please enter Security Answer\n"; if (contactnumber.length() == 0) msg += "Please enter Contact Number\n"; if (firstname.length() == 0) msg += "Please enter First name\n"; if (lastname.length() == 0) msg += "Please enter Last name\n"; if (genderSelected.length() == 0) msg += "Please enter Gender\n"; if (nric.length() == 0) msg += "Please enter NRIC\n"; if (address.length() == 0) msg += "Please enter Address\n"; if (postalcode1.length() == 0) msg += "Please enter Postal code\n"; if (date.length() == 0) msg += "Please enter Date\n"; if (msg.length() == 0) result = true; else JOptionPane.showMessageDialog(myFrame, msg, "Alert", msgType); return result; } /** * @wbp.parser.constructor */ public hiroSIGNUPPanel(JFrame mf, String action, hiroSIGNUP s){ this(mf, action); //display the current expense information signup = s; textFieldUsername.setText(s.getUsername()); passwordField.setText(s.getPassword()); textFieldEmail.setText(s.getEmail()); textFieldSecurityqn.setText(s.getSecurityqn()); textFieldSecurityanswer.setText(s.getSecurityanswer()); textFieldContactnum.setText(String.valueOf(s.getContactnum())); textFieldFirstName.setText(s.getFirstname()); textFieldLastName.setText(s.getLastname()); //textFieldGender.setText(s.getGender()); textFieldNRIC.setText(s.getNric()); textFieldAddress.setText(s.getAddress()); textFieldPostalCode.setText(String.valueOf(s.getPostalcode()));
}
}
and my error is this
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at hiroApp.ui.hiroSIGNUPPanel.actionPerformedOk(hiroSIGNUPPanel.java:781)
原文:https://stackoverflow.com/questions/28072913
最满意答案
你可以发布整个堆栈跟踪吗? 另外,为什么要在视图(JSP)中嵌入数据库连接逻辑? 他们应该在控制器的某个地方。
Can you post the entire stack trace? Also, why are you embedding DB connection logic in the view (JSP)? They should be in the controller somewhere.
相关问答
更多-
问题已经解决了。 可悲的是我不知道解决方案。 我所做的只是打开一个新项目并在那里使用代码。 在我这样做后,我可以调试没有问题的代码。 对不起,我无法帮助真正解决这个问题,并帮助社区解答我自己的问题。 感谢所有试图帮助的人,D. The problem is solved. The sad thing is that I don't know the solution. What I did was simply to open a new project and use the code there. Af ...
-
卡洛斯方法有帮助! Eclipse - java.lang.ClassNotFoundException 尝试检查junit运行配置的classpath: 打开运行配置 单击要启动的jUnit测试 转到类路径选项卡 尝试添加一个文件夹(单击用户条目,单击高级,单击添加文件夹,单击确定并搜索您的测试类的输出文件夹(那些您在projektproperties下的java构建路径,源)) 为我工作 I've come across that situation several times and, after a ...
-
“安装”是什么意思? 基本上,数据库服务器必须安装并运行。 第二步是添加驱动程序(可以在C:\Program Files\sdb\programs\runtime\jar\sapdbc.jar ,假设这也适用于您的安装)添加到项目的构建路径。 如果找不到驱动程序类,它会像您遇到的那样引发ClassNotFoundException 。 你没有提到你正在使用哪个IDE,但是尝试从上下文菜单中设置你的项目构建路径。 What does "install" mean? Basically, the db serv ...
-
导入的项目抛出java.lang.ClassNotFoundException(Imported project throws java.lang.ClassNotFoundException)[2022-03-19]
我相当肯定我已经看到这样的事情发生在我没有将代码导入Eclipse时,但是试图从其下载的位置运行库,例如Google Play游戏服务: 将Android示例中的TypeANumber和BaseGameUtils代码导入您的工作区。 要在Eclipse中执行此操作:单击文件>导入> Android>现有Android代码到工作区。 浏览到开发计算机上下载TypeANumber的目录,然后单击“确定”以导入文件。 对BaseGameUtils执行相同的步骤。 (我是从https://developers.go ... -
JSP页面不起作用:Java.lang.ClassNotFoundException(JSP page won't work :Java.lang.ClassNotFoundException)[2023-07-10]
你可以发布整个堆栈跟踪吗? 另外,为什么要在视图(JSP)中嵌入数据库连接逻辑? 他们应该在控制器的某个地方。 Can you post the entire stack trace? Also, why are you embedding DB connection logic in the view (JSP)? They should be in the controller somewhere. -
如果您已更新至ADTv17或更高版本,则旧的ProGuard文件将为您执行此操作。 创建一个新的空项目,并查看project.properties和proguard-properties并相应地修改当前项目。 If you have updated to ADTv17 or above, the old ProGuard files will do this to you. Create a new empty project, and see project.properties and proguard ...
-
您的项目类路径似乎缺少google-collect-1.0-rc1.jar 你显然在你的硬盘上有它,只需编辑项目类路径并在那里添加(或确保它由Maven /如果你使用它们添加)。 It seems that your project classpath is lacking google-collect-1.0-rc1.jar You obviously have it on your hard drive, just edit the project classpath and add it there ...
-
你的包是错的。 代码不会撒谎......这不存在。 com.work.pets.data.PetProvider data.PetProvider确实... 修复package data; 使用你的应用程序的包 Your package is wrong. The code doesn't lie... This doesn't exist. com.work.pets.data.PetProvider data.PetProvider does... Fix package data; to use ...
-
你能在工作流工作区的'lib'文件夹中添加所有依赖的jar吗? Could you add all the dependent jars in a 'lib' folder in the workspace of the workflow?
-
Apache Ant:java.lang.ClassNotFoundException:(Apache Ant: java.lang.ClassNotFoundException:)[2021-12-12]
尝试在类路径中添加selenium jar文件。 目前你只添加了junit。 Try adding selenium jar file in you classpath. Currently you only added junit.