Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
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 The Software Craftsman this week in the Agile 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
I agree. Here's the link:
subject: converting a string to an int