Converting a string variable to an integer variable in Java is easy. There are actually two different approaches, both equally simply. Below is an example of both approaches, with both use the same string variable to output to various integer variables.
String myVariable = “12345″;
Approach 1:
int approach1 = Integer.parseInt(myVariable);
Approach 2:
int approach2 = Integer.valueOf(myVariable);
If you have any opinion on why you use one approach over the other, let the community know!
RSS Feed
Posted in
Tags: 

Atleast write the headings properly. Its not converting String to Int, its the other way round.
This does convert a String to an int.
public int IsInteger(string data)
{
bool result = true;
try
{
int.Parse(data);
}
catch (FormatException)
{
result = false;
}
return result;
}
ex:how the code should look:
previous code:
scanf(“%d”,x) should be changed into
new code:
where x is a string now
where y is bool
where x is a int
do
{
//get the interger value
scanf(“%s”,x)
//use the function above to find wheather the given value is int or not.
y=IsInteger(x)
//if y is true i.e int then convert it into interger using atoi function.
if(y)
{
z=atoi(z);
}
else
{
printf(“error msg”);
//if false then scan the variable again
}while(!y)
nice information for me. thank you very much with that info.