You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Thomas <li...@jps.net> on 2000/08/21 08:36:43 UTC
How to cast JSP request String to int?
How do you use a number from a request parameter in an equation using JSP? In other words,
How do you set the value of an int variable to the value of a numeric String variable?
I tried to do:
int Repeat1__numRows = 10;
if(request.getParameter("num_results")!=null) {
Repeat1__numRows = (int)request.getParameter("num_results");
}
But the engine (tomcat 3.1 and jdk 1.3) says:
Invalid cast from java.lang.String to int
thanks for a tip,
Thomas
Re: How to cast JSP request String to int?
Posted by Yun Sang Jung <na...@channeli.net>.
try below code!
try {
Repeat1__numRows = Integer.parseInt(request.getParameter("num_results"));
} catch (NumberFormatExeption e) {
}
----- Original Message -----
From: Thomas
To: tomcat-user@jakarta.apache.org
Sent: Monday, August 21, 2000 3:36 PM
Subject: How to cast JSP request String to int?
How do you use a number from a request parameter in an equation using JSP? In other words,
How do you set the value of an int variable to the value of a numeric String variable?
I tried to do:
int Repeat1__numRows = 10;
if(request.getParameter("num_results")!=null) {
Repeat1__numRows = (int)request.getParameter("num_results");
}
But the engine (tomcat 3.1 and jdk 1.3) says:
Invalid cast from java.lang.String to int
thanks for a tip,
Thomas
Re: How to cast JSP request String to int?
Posted by Rachel Greenham <ra...@enetgroup.co.uk>.
> Thomas wrote:
>
> How do you use a number from a request parameter in an equation using
> JSP? In other words,
> How do you set the value of an int variable to the value of a numeric
> String variable?
You can't just cast one type to a completely different type in Java. It may
seem inconvenient when you're starting out, especially coming from other
languages, but it's a good thing really. Trust me.
This is proper behaviour for Java, not version-specific and not
Tomcat-specific.
What you need is this:
int Repeat1__numRows = 10;
String num_results = request.getParameter("num_results");
if (num_results != null)
{
try
{
Repeat1__numRows = Integer.parseInt(num_results);
}
catch (NumberFormatException nfe)
{
// Can't parse this as a number. Ignore and use default
// (Obvious opportunity to do other things here, like show an error)
}
}
--
Rachel