如何将字符串数组转换为int数组?

时间:2017-03-22 08:08:55

标签: java arrays numberformatexception

我得到NumberFormatException,如何将String[]转换为int[]?我得到的输入值是' text'对于年龄,我有五个相同年龄输入的输入字段。所以我使用数组来获得五个年龄。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {

        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        String[] name=request.getParameterValues("name");
        String[] relation=request.getParameterValues("relation");
        String[] age=request.getParameterValues("age");
        String[] occupation=request.getParameterValues("occupation");
        String[] education=request.getParameterValues("education");

        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sportsevent", "root", "root");

        PreparedStatement ps=con.prepareStatement("insert into multiple(name, relationship, age, occupation, education) values(?,?,?,?,?)");
        for (int i = 0; i < name.length; i++) {
            if(name[i] != null) {
            ps.setString(1, name[i]);
            ps.setString(2, relation[i]);
            ps.setInt(3, age[i]);
            ps.setString(4, occupation[i]);
            ps.setString(5, education[i]);
            }
        }
        ps.executeUpdate();

    }catch (Exception e){System.out.println(e);}
}

1 个答案:

答案 0 :(得分:1)

尝试这种方式;

ps.setInt(3, Integer.parseInt(age[i]));