Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cast a String to an int, how?

 
Shuaib Gill
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can someone tell me how to convert a type String to a type int. I have values in a hashtable and I am using the 'put' method to put a value in the hashtable. Here is the function
public void setAgehashtable( int i, int age)
{
this.age=age;
ageHashtable.put(new Integer(i),new Integer(age));
}
Then I'm calling the 'getAgehashtable( int i)' method, like so
public int getAgehashtable( int i)
{
ageInHashtableString=null;
ageInHashtableString=(String)ageHashtable.get(new Integer(i));
return (int)ageInHashtableString; // error , why???
}
I need to return the value as an integer, not String. I am trying to cast it from String to int, but an error results. Why? Please help.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try
return Integer.ParseInt(ageInHashtableString);
 
Shuaib Gill
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Cindy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic