• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting strings

 
Yasmeen G
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a beginner and I need help!
I would like to change a string into an integer. I've tried
int number = (int) j;//j is the string here.
I've tried other things and they don't work either. Surfing the web hasn't been helping me either.
Hope someone can help!
thanks.
 
Richard Mack
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, Strings are object references. It is illegal to cast/convert an object reference (J) to a primative (number).
What are you trying to accomplish this for?
 
Siobhan Murphy
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the following:
String j = "2024";
int i = Integer.parseInt(j);
 
Yasmeen G
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help! It worked. I was just constructing a simple program.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic