This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes converting a string to an int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "converting a string to an int" Watch "converting a string to an int" New topic

converting a string to an int

tyler jones
Ranch Hand

Joined: Dec 01, 2000
Posts: 101
I can't remember how to convert a string to an int. I have the variable x which has the value of "1". How can I then convert it to an int? I've tried something like...
int x = (int)x
int y = (int)x
but I always get errors. Thanks.
Ranch Hand

Joined: Mar 06, 2001
Posts: 123
How about
String x = "1";
int y = Integer.parseInt(x);

[This message has been edited by Prashanth Joisha (edited September 27, 2001).]
timothy zimmerman
Ranch Hand

Joined: Jun 26, 2001
Posts: 149
There are three methods that I could think of given
String x = "1"
new Integer(x).intValue()
and probably the preferred way as described in the previous post
It is sorta covered in the JavaRanch Style Guide.
subject: converting a string to an int
Similar Threads
convert int ,double to string
Converting user input
java.util.Vector --> byte[] ?
conversion from String to any data type