Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having trouble converting from string to in

Status
Not open for further replies.

daph

Programmer
Jun 9, 2000
65
CA
Hi everyone,

I'm new to JSP and I've been trying to do some basic stuff to get used to it. Now, I've been trying to convert a string to a int, but no matter how I try it, I get errors. What package do I need to import for this? What function should I use? I tried Integer.parseInt but that didn't work either.

I was also wondering how I can import more than one package with this line
<%@ page contentType=&quot;text/html&quot; import=&quot;java.util.*&quot; %>
Do I have to add more than one line or can I add more into this one?

Thanks for all your help...I really appreciate it!

Daph
 
Integer.parseInt is definitely what you want. Make sure that you catch a NumberFormatException in case the input is invalid.
Code:
String s = &quot;2&quot;;
int i;
try {
  i = Integer.parseInt(s);
}
catch(NumberFormatException nfe) {
  // invalid number
}
To import more than one package just separate with commas.
Code:
<%@page language=&quot;java&quot; import=&quot;java.util.*,java.text.*&quot; %>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top