wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: cast a String to an int, how?
 
Similar Threads
Retrieving hashtable values in JSP
Netbeans jax-ws generated sources erroring on method invocation.
Loosing Grey Matter
from int to Integer casting?
hashmap and equals doubts