Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Type Casting

Status
Not open for further replies.

robmif76

Programmer
Feb 1, 2003
10
GB
Hi all,

Is it possible to type cast a String variable into an integer?

I'm tryong with the following code but I'm getting an error on compilation saying inconvertible types.

void yearsWorked(String sWorkYear)
{
int iYrsWorked;

iYrsWorked = (int) sWorkYear;
}

Cheers
Rob
 
This is the correct way:

iYrsWorked = Integer.parseInt(sWorkYear);
 
Hi Rob,

u can convert a string to int using the way suggested by Stijn147, but it will be better if u put it in a try-catch block, as it can throw NumberFormatException. The more complete way thus is:-

try{
int intIntegerForm = Integer.parseInt(strStringForm);
}catch(NumberFormatException objNumberFomatException){
objNumberFomatException.printStackTrace();
}

Prakash Kanodia
 
Thanks a lot people!!
again......problem solved!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top