Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes cast a String to an int, how? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cast a String to an int, how?" Watch "cast a String to an int, how?" New topic
Author

cast a String to an int, how?

Shuaib Gill
Ranch Hand

Joined: May 29, 2001
Posts: 62
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.

programmer77
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Try
return Integer.ParseInt(ageInHashtableString);

"JavaRanch, where the deer and the Certified play" - David O'Meara
Shuaib Gill
Ranch Hand

Joined: May 29, 2001
Posts: 62
Thank you Cindy
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: cast a String to an int, how?