+ 20
How do I convert a String to an int in Java ?
My String contains only numbers, and I want to return the number it represents. For example,given the String "1234" result should be the number 1234.
11 Answers
+ 19
yes , there is one more
//which i know
public class Program{
public static void main(String[] args) {
int a=Integer.valueOf("1234")-2;
System.out.println(a);
}
}
//diff. is of the return type most probably
+ 29
int a=Integer.parseInt("1234");
+ 18
thank you @Don't Mess With Texas and thank you @Gaurav Agrawal
+ 16
âșđ
+ 14
String mystr="1234";
int a = Integer.parseInt(mystr);
is there any different way to do that except parseInt?
+ 5
parseInt("1234");
+ 3
what is the use valueOf?!?!
+ 3
thank you đđ@Gaurav
+ 3
String love="143";
Int x= integer. parseInt(love) ;
+ 1
I have a very good buddy of mind that I would love for you to meet... His name is JSON.
He's lightweight, and surprisingly useful for many many unexpected ways such as this preticular situation.
JSON is the kind of man whos got your back, when your cornered by knife wielding crusted gutter punks looking to skin you alive. You think to yourself... This must be the end of for us. JSON reaches into his trench coat for a fresh dart, and ignites it with his limited edition phub themed zippo. JSON inhales a very deep drag, and then suddenly hunches over starts coughing out of control. JSON slowly turns his head towards you and says " Oh yeah, I forgot... I don't even smoke".
// To be continued!
+ 1
we can use wrapper classes to convert String to int
for example :
String s ="6789";
int num = Integer. parseInt(s) ;
System. out. print(num) ;
output: 6789