Java 无法将字符串转换为整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18458877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:48:10  来源:igfitidea点击:

Can't convert String to Integer

javamysqljsp

提问by Salini L

I am a beginner. I know this is basic. In my project I am using Java and MySQL workbench. I am reading a data from user by using select box which is coming from database by using ajax. The code for select box is mentioned below

我是初学者。我知道这是基本的。在我的项目中,我使用 Java 和 MySQL 工作台。我正在通过使用 ajax 来自数据库的选择框从用户读取数据。下面提到了选择框的代码

<%
 String a =request.getParameter("course");
 if(a!=null)
   {
       ResultSet rs=s.selectsub(a);
       String Query="select * from  subject where course_id='"+a+"'";
 %>
 <select name="subject" id="subject">
   <option>Select Subject</option>
   <%
    while(rs.next())
    {
    %>
    <option value="<% out.println(rs.getString("subject_id")); %>">
    <% out.println(rs.getString("subject")); %></option>
   <% } %>
  </select>
<%
}
%>

and the subject id is passed to another page using post method and trying this code

并且使用 post 方法将主题 ID 传递到另一个页面并尝试此代码

     String subject=request.getParameter("subject");
     int subjectid=Integer.parseInt(subject);

But the line Integer conversion is not working. Error is showing. The error is

但是行整数转换不起作用。错误显示。错误是

org.apache.jasper.JasperException: An exception occurred processing JSP page /saveuser.jsp at line 29
26:          String email=request.getParameter("email");
27:          String designation=request.getParameter("designation");
28:          String subject=request.getParameter("subject");
29:          int subjectid=Integer.parseInt(subject);
30:          String institute=request.getParameter("institute");
31:          String inemail=request.getParameter("inemail");
32:          String uname=request.getParameter("uname");

采纳答案by Juned Ahsan

Integer.parseIntmethod will convert a valid integer value string to an int. Otherwise it will throw an exception. So make sure you pass a valid integer value to it. Sometimes the trailing space in the string causes exception. So call trim method on the input string to avoid that:

Integer.parseInt方法会将有效的整数值字符串转换为 int。否则会抛出异常。因此,请确保将有效的整数值传递给它。有时字符串中的尾随空格会导致异常。所以在输入字符串上调用 trim 方法来避免这种情况:

 int subjectid=Integer.parseInt(subject.trim());

Also make sure that subject is not null. So this seems better:

还要确保主题不为空。所以这看起来更好:

 String subject=request.getParameter("subject");
 int subjectid = 0;
 if(subject !=null && !subject.isEmpty())
     subjectid=Integer.parseInt(subject.trim());

回答by vikingsteve

It looks like you are mixing up your data types, trying to convert getParameter("subject")to an Integer.

看起来您正在混淆数据类型,试图转换getParameter("subject")为整数。

Perhaps you have a parameter named "subject_id", and could this work?

也许您有一个名为 的参数"subject_id",这可以工作吗?

 String subject_id_string=request.getParameter("subject_id");
 int subjectid=Integer.parseInt(subject_id_string);

回答by Sri Harsha Chilakapati

Use Integer.parseInt(String)method.

使用Integer.parseInt(String)方法。

int subjectid = Integer.parseInt(subject.trim());

From the documentation

从文档

Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.

将字符串参数解析为有符号十进制整数。字符串中的字符必须都是十进制数字,除了第一个字符可以是 ASCII 减号 '-' ('\u002D') 表示负值或 ASCII 加号 '+' ('\u002B')表示正值。返回结果整数值,就像参数和基数 10 作为参数提供给 parseInt(java.lang.String, int) 方法一样。