Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String to variable - urgent!  RSS feed

 
James Hewitt
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, this question probably has a really obvious answer, but I'm a beginner, so bear with me. I need to make my servlet perform a calculation on values obtained from an html input form. I import the values from the form as a string using, e.g.:
String Value = req.getParameter("Value");
However, I cannot then perform any calculations on it, since it is a string, not an integer, for example, nor can I find a way to convert it to an integer. Can anyone help? Quickly would be real good.
Thanks,
James
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldn't you just use Integer.parseInt(String) or Integer.getInteger(String)? Let me know if that helps.
[This message has been edited by Sean Casey (edited July 09, 2001).]
 
Peter Thomas
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like the right kind of thing, but I can't get it to work. When I said beginner, I really mean BEGINNER. Below is the code I'm trying to adapt. What I'd like to do is to make "value" (this is the name of the field from the html form) an integer, and then multiply it by 2 or another variable (for example) and display this in the HTML output. Any chance you could enter it into the code below? That would be excellent. Sorry to be so useless
{
res.setContentType("text/html");
String User = req.getParameter("Value");

ServletOutputStream out = res.getOutputStream();
out.println("<html>");
out.println("<head><title>Hello User!</title></head>");
out.println("<body>");
System.out.println("Value");
out.println("</body></html>");
}
 
James Hewitt
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, went on to my friends user there. Confusing. Sorry
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean gave the answer.
If I were a Java instructor (which I am not), I would ask you how come you're already into coding Java Servlets (which is like Java 201) without a mastery of the very basics of Java 101:
static int parseInt(String s) throws NumberFormatException
BTW, Don't forget to handle the Exception
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!