How do I convert a String to an int in Java ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

2nd Mar 2018, 9:42 AM
Ali Han Demir
Ali Han Demir - avatar
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
2nd Mar 2018, 9:48 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 29
int a=Integer.parseInt("1234");
2nd Mar 2018, 9:45 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 18
thank you @Don't Mess With Texas and thank you @Gaurav Agrawal
2nd Mar 2018, 9:48 AM
Ali Han Demir
Ali Han Demir - avatar
+ 16
ā˜ŗšŸ‘
2nd Mar 2018, 9:49 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 14
String mystr="1234"; int a = Integer.parseInt(mystr); is there any different way to do that except parseInt?
2nd Mar 2018, 9:44 AM
Ali Han Demir
Ali Han Demir - avatar
+ 5
parseInt("1234");
2nd Mar 2018, 9:44 AM
Kevin Eldurson
Kevin Eldurson - avatar
+ 3
what is the use valueOf?!?!
2nd Mar 2018, 9:50 AM
Code_Magicianā¤
Code_Magicianā¤ - avatar
+ 3
thank you šŸ˜ŠšŸ˜Š@Gaurav
2nd Mar 2018, 9:53 AM
Code_Magicianā¤
Code_Magicianā¤ - avatar
+ 3
String love="143"; Int x= integer. parseInt(love) ;
6th Mar 2018, 9:30 PM
Ahmad Ishaq
Ahmad Ishaq - avatar
+ 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!
14th Apr 2021, 3:56 AM
DARC
DARC - avatar
+ 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
13th Jul 2021, 7:34 PM
Abhishek Dwivedi
Abhishek Dwivedi - avatar