我正在尝试将JTextField中接受的roll no转换为整数值,以便它可以在MySQL中使用,但每当我点击提交按钮时,会弹出一个错误
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 random.student$1.actionPerformed(student.java:141)
这是我用过的代码
package random;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class student extends JFrame{
int N = 0;
int i,j; // instance variables
String namex[] = new String[10]; // used in jdbc
String course[] = new String[10];
int roll_no[] = new int[10];
// jdbc
Connection conn = null;
PreparedStatement stmt = null;
public student() {
super("Student");
setLayout(null); // when layout is null, setBounds method is used with every window component to set the location and dimensions of every object
String no = JOptionPane.showInputDialog(null,"Enter the number of students"); // data is accepted in form of String
N = Integer.parseInt(no); // changes string to integer
JLabel lbl[] = new JLabel[N];
JLabel top[] = new JLabel[4];
JTextField name[] = new JTextField[N]; // Name
JTextField crse[] = new JTextField[N]; // Course
JTextField roll[] = new JTextField[N]; // roll no
// title bar
top[0] = new JLabel("SNo");
top[0].setBounds(200,8,50,30);
add(top[0]);
top[1] = new JLabel("Name");
top[1].setBounds(250,8,50,30);
add(top[1]);
top[2] = new JLabel("Roll No");
top[2].setBounds(380,8,50,20);
add(top[2]);
top[3] = new JLabel("Course");
top[3].setBounds(450,8,50,20);
add(top[3]);
// SNo
for(i = 0 ; i < N ; i++) {
lbl[i] = new JLabel((i+1)+"");
lbl[i].setBounds(200, j+30, 50, 30);
add(lbl[i]);
j = j + 30; // height component
}
// Name fields
j = 0;
for(i = 0 ; i < N ; i++) {
name[i] = new JTextField(30);
name[i].setBounds(250, j+30, 100, 20);
add(name[i]);
j = j + 30;
}
// roll no
j = 0;
for(i = 0 ; i < N ; i++) {
roll[i] = new JTextField(10);
roll[i].setBounds(380,j+30,50,20);
add(roll[i]);
j = j + 30;
}
// course
j = 0 ;
for(i = 0 ; i < N ; i++) {
crse[i] = new JTextField(20);
crse[i].setBounds(450, j + 30, 50, 20);
add(crse[i]);
j+= 30;
}
JButton btn = new JButton("Commit");
btn.setBounds(400,300,100,40);
add(btn);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent exp) {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root",".dpadpep");
String sql = "Insert into student(roll_no,name,course) values(?,?,?)";
// Convert JTextField into String and integers
stmt = conn.prepareStatement(sql);
for(i = 0 ; i < N ; i++) {
namex[i] = name[i].getText(); // convert TextField to String
course[i] = crse[i].getText();
String text = roll[i].getText();
int rollno = Integer.parseInt(text); // first convert TextFIeld yo string and then into integer
stmt.setInt(1, rollno);
stmt.setString(2, namex[i]);
stmt.setString(3, course[i]);
}
}catch(Exception ex) {
ex.printStackTrace();
}
}
});
setLocation(100, 100);
setVisible(true);
setSize(600,450);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new student();
}
}
我用不同的东西编辑了这段代码。该表已更新,但每当我点击提交时,也会弹出错误。 请帮助。
我无法将textfield中的String值转换为整数值。我之前已经完成了TextField不是一个数组但它们是单独工作的。
答案 0 :(得分:0)
这是因为当没有给出字符串(你没有输入任何内容并按下提交)时,java无法将该文本转换为整数(想象一下必须将任何内容/某些乱码转换为int),这就是错误说明的原因
输入字符串:“” )
您可以做的是设置Roll No字段的默认值,如0.接下来,验证Roll No的输入,以便只有数字可以插入Roll No文本字段。
希望这会有所帮助。