Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

converting a string to an int

 
tyler jones
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
and
int y = (int)x
but I always get errors. Thanks.
 
pjoisha
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about
String x = "1";
int y = Integer.parseInt(x);
PJ

[This message has been edited by Prashanth Joisha (edited September 27, 2001).]
 
timothy zimmerman
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are three methods that I could think of given
String x = "1"
new Integer(x).intValue()
Integer.valueOf(x).intValue()
and probably the preferred way as described in the previous post
Integer.parseInt(x)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic